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
|
|---|---|---|---|---|---|---|---|---|---|---|
AffineConstantFlow
|
import torch
from torch import nn
class AffineConstantFlow(nn.Module):
"""
Scales + Shifts the flow by (learned) constants per dimension.
In NICE paper there is a Scaling layer which is a special case of this where t is None
"""
def __init__(self, dim, scale=True, shift=True):
super().__init__()
self.s = nn.Parameter(torch.randn(1, dim, requires_grad=True)
) if scale else None
self.t = nn.Parameter(torch.randn(1, dim, requires_grad=True)
) if shift else None
def forward(self, x):
s = self.s if self.s is not None else x.new_zeros(x.size())
t = self.t if self.t is not None else x.new_zeros(x.size())
z = x * torch.exp(s) + t
log_det = torch.sum(s, dim=1)
return z, log_det
def backward(self, z):
s = self.s if self.s is not None else z.new_zeros(z.size())
t = self.t if self.t is not None else z.new_zeros(z.size())
x = (z - t) * torch.exp(-s)
log_det = torch.sum(-s, dim=1)
return x, log_det
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'dim': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_exp_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
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp2 = tl_math.exp(tmp1)
tmp3 = tmp0 * tmp2
tmp5 = tmp3 + tmp4
tl.store(out_ptr0 + x2, tmp5, xmask)
@triton.jit
def triton_per_fused_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 + 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):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (1, 4), (4, 1))
assert_size_stride(primals_2, (1, 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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_exp_mul_0[grid(256)](primals_3, primals_1,
primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf1 = empty_strided_cuda((1,), (1,), torch.float32)
triton_per_fused_sum_1[grid(1)](primals_1, buf1, 1, 4, XBLOCK=1,
num_warps=2, num_stages=1)
return buf0, buf1, primals_1, primals_3
class AffineConstantFlowNew(nn.Module):
"""
Scales + Shifts the flow by (learned) constants per dimension.
In NICE paper there is a Scaling layer which is a special case of this where t is None
"""
def __init__(self, dim, scale=True, shift=True):
super().__init__()
self.s = nn.Parameter(torch.randn(1, dim, requires_grad=True)
) if scale else None
self.t = nn.Parameter(torch.randn(1, dim, requires_grad=True)
) if shift else None
def backward(self, z):
s = self.s if self.s is not None else z.new_zeros(z.size())
t = self.t if self.t is not None else z.new_zeros(z.size())
x = (z - t) * torch.exp(-s)
log_det = torch.sum(-s, dim=1)
return x, log_det
def forward(self, input_0):
primals_1 = self.s
primals_2 = self.t
primals_3 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0], output[1]
|
ilkhem/icebeem
|
AffineConstantFlow
| false
| 15,591
|
[
"MIT"
] | 48
|
0077f0120c83bcc6d9b199b831485c42bed2401f
|
https://github.com/ilkhem/icebeem/tree/0077f0120c83bcc6d9b199b831485c42bed2401f
|
SomeQNet
|
import torch
import torch as t
import torch.nn as nn
class SomeQNet(nn.Module):
def __init__(self, state_dim, action_num):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_num)
def forward(self, state):
a = t.relu(self.fc1(state))
a = t.relu(self.fc2(a))
return self.fc3(a)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_num': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (16, 4), (4, 1))
assert_size_stride(primals_2, (16,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (16, 16), (16, 1))
assert_size_stride(primals_5, (16,), (1,))
assert_size_stride(primals_6, (4, 16), (16, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf0
buf6 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1,
primals_2, buf6, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 16), (16, 1), 0),
reinterpret_tensor(primals_4, (16, 16), (1, 16), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf2
buf5 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf3,
primals_5, buf5, 1024, 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, 16),
(16, 1), 0), reinterpret_tensor(primals_6, (16, 4), (1, 16), 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, 16), (16, 1), 0), reinterpret_tensor(
buf3, (64, 16), (16, 1), 0), primals_6, buf5, primals_4, buf6
class SomeQNetNew(nn.Module):
def __init__(self, state_dim, action_num):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_num)
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]
|
iffiX/machin
|
SomeQNet
| false
| 15,592
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
ScaledDotProductAttention
|
import torch
import torch.optim.lr_scheduler
import torch.nn as nn
class ScaledDotProductAttention(nn.Module):
def __init__(self, d_model, attention_dropout=0.1):
super(ScaledDotProductAttention, self).__init__()
self.temper = d_model ** 0.5
self.dropout = nn.Dropout(attention_dropout)
self.softmax = nn.Softmax(dim=-1)
def forward(self, q, k, v, attn_mask=None):
attn = torch.bmm(q, k.transpose(1, 2)) / self.temper
if attn_mask is not None:
assert attn_mask.size() == attn.size(
), 'Attention mask shape {} mismatch with Attention logit tensor shape {}.'.format(
attn_mask.size(), attn.size())
attn.data.masked_fill_(attn_mask, -float('inf'))
attn = self.softmax(attn)
attn = self.dropout(attn)
output = torch.bmm(attn, v)
return output, attn
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4])
]
def get_init_inputs():
return [[], {'d_model': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.optim.lr_scheduler
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
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):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4), (16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(arg1_1, reinterpret_tensor(arg0_1, (4, 4, 4), (
16, 1, 4), 0), out=buf0)
del arg0_1
del arg1_1
buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__softmax_0[grid(64)](buf0, buf1, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf2 = buf0
del buf0
triton_poi_fused__softmax_1[grid(64)](buf1, buf2, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf3 = buf1
del buf1
extern_kernels.bmm(buf2, arg2_1, out=buf3)
del arg2_1
return buf3, buf2
class ScaledDotProductAttentionNew(nn.Module):
def __init__(self, d_model, attention_dropout=0.1):
super(ScaledDotProductAttentionNew, self).__init__()
self.temper = d_model ** 0.5
self.dropout = nn.Dropout(attention_dropout)
self.softmax = nn.Softmax(dim=-1)
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]
|
interrogator/self-attentive-parser
|
ScaledDotProductAttention
| false
| 15,593
|
[
"MIT"
] | 88
|
660d0161cb6ec6455d1525d029ff09362dcf7faf
|
https://github.com/interrogator/self-attentive-parser/tree/660d0161cb6ec6455d1525d029ff09362dcf7faf
|
QNet
|
import torch
import torch as t
import torch.nn as nn
class QNet(nn.Module):
def __init__(self, state_dim, action_num, atom_num=10):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_num * atom_num)
self.action_num = action_num
self.atom_num = atom_num
def forward(self, state):
a = t.relu(self.fc1(state))
a = t.relu(self.fc2(a))
return t.softmax(self.fc3(a).view(-1, self.action_num, self.
atom_num), dim=-1)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_num': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_per_fused__softmax_1(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
tl.constexpr):
xnumel = 256
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 = tmp6 / tmp10
tl.store(out_ptr2 + (r1 + 10 * x0), tmp11, rmask & xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (16, 4), (4, 1))
assert_size_stride(primals_2, (16,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (16, 16), (16, 1))
assert_size_stride(primals_5, (16,), (1,))
assert_size_stride(primals_6, (40, 16), (16, 1))
assert_size_stride(primals_7, (40,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf0
buf9 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1,
primals_2, buf9, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 16), (16, 1), 0),
reinterpret_tensor(primals_4, (16, 16), (1, 16), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf2
buf8 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf3,
primals_5, buf8, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_5
buf4 = empty_strided_cuda((64, 40), (40, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 16),
(16, 1), 0), reinterpret_tensor(primals_6, (16, 40), (1, 16), 0
), alpha=1, beta=1, out=buf4)
del primals_7
buf7 = empty_strided_cuda((64, 4, 10), (40, 10, 1), torch.float32)
triton_per_fused__softmax_1[grid(256)](buf4, buf7, 256, 10, XBLOCK=
32, num_warps=4, num_stages=1)
del buf4
return buf7, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 16), (16, 1), 0), reinterpret_tensor(
buf3, (64, 16), (16, 1), 0), buf7, primals_6, buf8, primals_4, buf9
class QNetNew(nn.Module):
def __init__(self, state_dim, action_num, atom_num=10):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_num * atom_num)
self.action_num = action_num
self.atom_num = atom_num
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]
|
iffiX/machin
|
QNet
| false
| 15,594
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
LinearExcitability
|
import math
import torch
from torch import nn
from torch.nn.parameter import Parameter
def linearExcitability(input, weight, excitability=None, bias=None):
"""Applies a linear transformation to the incoming data: :math:`y = c(xA^T) + b`.
Shape:
- input: :math:`(N, *, in_features)`
- weight: :math:`(out_features, in_features)`
- excitability: :math:`(out_features)`
- bias: :math:`(out_features)`
- output: :math:`(N, *, out_features)`
(NOTE: `*` means any number of additional dimensions)"""
if excitability is not None:
output = input.matmul(weight.t()) * excitability
else:
output = input.matmul(weight.t())
if bias is not None:
output += bias
return output
class LinearExcitability(nn.Module):
"""Module for a linear transformation with multiplicative excitability-parameter (i.e., learnable) and/or -buffer.
Args:
in_features: size of each input sample
out_features: size of each output sample
bias: if 'False', layer will not learn an additive bias-parameter (DEFAULT=True)
excitability: if 'True', layer will learn a multiplicative excitability-parameter (DEFAULT=False)
excit_buffer: if 'True', layer will have excitability-buffer whose value can be set (DEFAULT=False)
Shape:
- input: :math:`(N, *, in_features)` where `*` means any number of additional dimensions
- output: :math:`(N, *, out_features)` where all but the last dimension are the same shape as the input.
Attributes:
weight: the learnable weights of the module of shape (out_features x in_features)
excitability: the learnable multiplication terms (out_features)
bias: the learnable bias of the module of shape (out_features)
excit_buffer: fixed multiplication variable (out_features)"""
def __init__(self, in_features, out_features, bias=True, excitability=
False, excit_buffer=False):
super(LinearExcitability, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(out_features, in_features))
if excitability:
self.excitability = Parameter(torch.Tensor(out_features))
else:
self.register_parameter('excitability', None)
if bias:
self.bias = Parameter(torch.Tensor(out_features))
else:
self.register_parameter('bias', None)
if excit_buffer:
buffer = torch.Tensor(out_features).uniform_(1, 1)
self.register_buffer('excit_buffer', buffer)
else:
self.register_buffer('excit_buffer', None)
self.reset_parameters()
def reset_parameters(self):
"""Modifies the parameters "in-place" to initialize / reset them at appropriate values."""
stdv = 1.0 / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.excitability is not None:
self.excitability.data.uniform_(1, 1)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input):
"""Running this model's forward step requires/returns:
-[input]: [batch_size]x[...]x[in_features]
-[output]: [batch_size]x[...]x[hidden_features]"""
if self.excit_buffer is None:
excitability = self.excitability
elif self.excitability is None:
excitability = self.excit_buffer
else:
excitability = self.excitability * self.excit_buffer
return linearExcitability(input, self.weight, excitability, self.bias)
def __repr__(self):
return self.__class__.__name__ + '(' + 'in_features=' + str(self.
in_features) + ', out_features=' + str(self.out_features) + ')'
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import math
from torch import nn
from torch.nn.parameter import Parameter
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_view_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
x4 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x4, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x4, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
buf2 = buf1
del buf1
get_raw_stream(0)
triton_poi_fused_add_view_0[grid(256)](buf2, primals_2, 256, XBLOCK
=256, num_warps=4, num_stages=1)
del primals_2
return buf2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0)
def linearExcitability(input, weight, excitability=None, bias=None):
"""Applies a linear transformation to the incoming data: :math:`y = c(xA^T) + b`.
Shape:
- input: :math:`(N, *, in_features)`
- weight: :math:`(out_features, in_features)`
- excitability: :math:`(out_features)`
- bias: :math:`(out_features)`
- output: :math:`(N, *, out_features)`
(NOTE: `*` means any number of additional dimensions)"""
if excitability is not None:
output = input.matmul(weight.t()) * excitability
else:
output = input.matmul(weight.t())
if bias is not None:
output += bias
return output
class LinearExcitabilityNew(nn.Module):
"""Module for a linear transformation with multiplicative excitability-parameter (i.e., learnable) and/or -buffer.
Args:
in_features: size of each input sample
out_features: size of each output sample
bias: if 'False', layer will not learn an additive bias-parameter (DEFAULT=True)
excitability: if 'True', layer will learn a multiplicative excitability-parameter (DEFAULT=False)
excit_buffer: if 'True', layer will have excitability-buffer whose value can be set (DEFAULT=False)
Shape:
- input: :math:`(N, *, in_features)` where `*` means any number of additional dimensions
- output: :math:`(N, *, out_features)` where all but the last dimension are the same shape as the input.
Attributes:
weight: the learnable weights of the module of shape (out_features x in_features)
excitability: the learnable multiplication terms (out_features)
bias: the learnable bias of the module of shape (out_features)
excit_buffer: fixed multiplication variable (out_features)"""
def __init__(self, in_features, out_features, bias=True, excitability=
False, excit_buffer=False):
super(LinearExcitabilityNew, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(out_features, in_features))
if excitability:
self.excitability = Parameter(torch.Tensor(out_features))
else:
self.register_parameter('excitability', None)
if bias:
self.bias = Parameter(torch.Tensor(out_features))
else:
self.register_parameter('bias', None)
if excit_buffer:
buffer = torch.Tensor(out_features).uniform_(1, 1)
self.register_buffer('excit_buffer', buffer)
else:
self.register_buffer('excit_buffer', None)
self.reset_parameters()
def reset_parameters(self):
"""Modifies the parameters "in-place" to initialize / reset them at appropriate values."""
stdv = 1.0 / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.excitability is not None:
self.excitability.data.uniform_(1, 1)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def __repr__(self):
return self.__class__.__name__ + '(' + 'in_features=' + str(self.
in_features) + ', out_features=' + str(self.out_features) + ')'
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]
|
ifgovh/continual-learning
|
LinearExcitability
| false
| 15,595
|
[
"MIT"
] | 891
|
21822801934ad68ca311c1c30ae49cdbd7ca53ed
|
https://github.com/ifgovh/continual-learning/tree/21822801934ad68ca311c1c30ae49cdbd7ca53ed
|
A2CActorCont
|
import torch
import torch as t
import torch.nn as nn
from torch.distributions import Normal
import torch.nn.functional as F
class A2CActorCont(nn.Module):
def __init__(self, state_dim, action_dim, action_range):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.mu_head = nn.Linear(16, action_dim)
self.sigma_head = nn.Linear(16, action_dim)
self.action_range = action_range
def forward(self, state, action=None):
a = t.relu(self.fc1(state))
a = t.relu(self.fc2(a))
mu = 2.0 * t.tanh(self.mu_head(a))
sigma = F.softplus(self.sigma_head(a))
dist = Normal(mu, sigma)
action = action if action is not None else dist.sample()
action_entropy = dist.entropy()
action = action.clamp(-self.action_range, self.action_range)
action_log_prob = dist.log_prob(action)
return action, action_log_prob, action_entropy
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_dim': 4, 'action_range': 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_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_mul_tanh_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = libdevice.tanh(tmp0)
tmp2 = 2.0
tmp3 = tmp1 * tmp2
tl.store(out_ptr0 + x0, tmp3, xmask)
@triton.jit
def triton_poi_fused_add_log_softplus_2(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 = 20.0
tmp2 = tmp0 > tmp1
tmp3 = tl_math.exp(tmp0)
tmp4 = libdevice.log1p(tmp3)
tmp5 = tl.where(tmp2, tmp0, tmp4)
tmp6 = tl_math.log(tmp5)
tmp7 = 1.4189385332046727
tmp8 = tmp6 + tmp7
tl.store(out_ptr0 + x0, tmp5, xmask)
tl.store(out_ptr1 + x0, tmp8, xmask)
@triton.jit
def triton_poi_fused_clamp_div_log_mul_neg_pow_sub_3(in_out_ptr0,
in_out_ptr1, 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_out_ptr0 + x0, xmask)
tmp5 = tl.load(in_out_ptr1 + x0, xmask)
tmp7 = tl.load(in_ptr0 + x0, xmask)
tmp1 = -4.0
tmp2 = triton_helpers.maximum(tmp0, tmp1)
tmp3 = 4.0
tmp4 = triton_helpers.minimum(tmp2, tmp3)
tmp6 = tmp4 - tmp5
tmp8 = tmp7 * tmp7
tmp9 = 2.0
tmp10 = tmp8 * tmp9
tmp11 = tmp6 * tmp6
tmp12 = -tmp11
tmp13 = tmp12 / tmp10
tmp14 = tl_math.log(tmp7)
tmp15 = tmp13 - tmp14
tmp16 = 0.9189385332046727
tmp17 = tmp15 - tmp16
tl.store(in_out_ptr0 + x0, tmp4, xmask)
tl.store(in_out_ptr1 + x0, tmp6, xmask)
tl.store(out_ptr0 + x0, tmp10, xmask)
tl.store(out_ptr1 + x0, 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, (16, 4), (4, 1))
assert_size_stride(primals_2, (16,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (16, 16), (16, 1))
assert_size_stride(primals_5, (16,), (1,))
assert_size_stride(primals_6, (4, 16), (16, 1))
assert_size_stride(primals_7, (4,), (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((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf0
buf16 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1,
primals_2, buf16, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 16), (16, 1), 0),
reinterpret_tensor(primals_4, (16, 16), (1, 16), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf2
buf15 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf3,
primals_5, buf15, 1024, 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, 16),
(16, 1), 0), reinterpret_tensor(primals_6, (16, 4), (1, 16), 0),
alpha=1, beta=1, out=buf4)
del primals_7
buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_9, reinterpret_tensor(buf3, (64, 16),
(16, 1), 0), reinterpret_tensor(primals_8, (16, 4), (1, 16), 0),
alpha=1, beta=1, out=buf5)
del primals_9
buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_mul_tanh_1[grid(256)](buf4, buf6, 256, XBLOCK=256,
num_warps=4, 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.float32)
triton_poi_fused_add_log_softplus_2[grid(256)](buf5, buf7, buf10,
256, XBLOCK=256, num_warps=4, num_stages=1)
buf8 = torch.ops.aten.normal.Tensor_Tensor(buf6, buf7)
buf9 = buf8
del buf8
buf11 = buf9
del buf9
buf12 = buf6
del buf6
buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
buf14 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_clamp_div_log_mul_neg_pow_sub_3[grid(256)](buf11,
buf12, buf7, buf13, buf14, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del buf7
return buf11, buf14, buf10, reinterpret_tensor(primals_3, (64, 4), (4,
1), 0), reinterpret_tensor(buf1, (64, 16), (16, 1), 0
), reinterpret_tensor(buf3, (64, 16), (16, 1), 0
), buf4, buf5, buf12, buf13, primals_8, primals_6, buf15, primals_4, buf16
class A2CActorContNew(nn.Module):
def __init__(self, state_dim, action_dim, action_range):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.mu_head = nn.Linear(16, action_dim)
self.sigma_head = nn.Linear(16, action_dim)
self.action_range = action_range
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.mu_head.weight
primals_7 = self.mu_head.bias
primals_8 = self.sigma_head.weight
primals_9 = self.sigma_head.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9])
return output[0], output[1], output[2]
|
iffiX/machin
|
A2CActorCont
| false
| 15,596
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
ActorDiscrete
|
import torch
import torch as t
import torch.nn as nn
class ActorDiscrete(nn.Module):
def __init__(self, state_dim, action_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_dim)
def forward(self, state):
a = t.relu(self.fc1(state))
a = t.relu(self.fc2(a))
a = t.softmax(self.fc3(a), dim=1)
return a
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused__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, (16, 4), (4, 1))
assert_size_stride(primals_2, (16,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (16, 16), (16, 1))
assert_size_stride(primals_5, (16,), (1,))
assert_size_stride(primals_6, (4, 16), (16, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf0
buf8 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1,
primals_2, buf8, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 16), (16, 1), 0),
reinterpret_tensor(primals_4, (16, 16), (1, 16), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf2
buf7 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf3,
primals_5, buf7, 1024, 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, 16),
(16, 1), 0), reinterpret_tensor(primals_6, (16, 4), (1, 16), 0),
alpha=1, beta=1, out=buf4)
del primals_7
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(256)](buf4, buf5, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf6 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf4
triton_poi_fused__softmax_2[grid(256)](buf5, buf6, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf5
return buf6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 16), (16, 1), 0), reinterpret_tensor(
buf3, (64, 16), (16, 1), 0), buf6, primals_6, buf7, primals_4, buf8
class ActorDiscreteNew(nn.Module):
def __init__(self, state_dim, action_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_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]
|
iffiX/machin
|
ActorDiscrete
| false
| 15,597
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
PARALoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class PARALoss(nn.Module):
"""
Softmax classifier for sentence-level relation extraction.
"""
def __init__(self):
"""
Args:
sentence_encoder: encoder for sentences
num_class: number of classes
id2rel: dictionary of id -> relation name mapping
"""
super().__init__()
def forward(self, score, predicate_one_hot_labels):
entity_mask = predicate_one_hot_labels.sum(dim=1, keepdim=True
).repeat_interleave(score.shape[1], dim=1)
entity_mask = (entity_mask > 0).float()
entity_sum = (entity_mask != 0).sum(dim=(2, 3)).float()
loss = ((F.binary_cross_entropy(score, predicate_one_hot_labels,
reduction='none') * entity_mask).sum(dim=(2, 3)) / entity_sum
).mean()
if loss.item() < 0:
None
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__to_copy_binary_cross_entropy_gt_mul_ne_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
x3 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
tmp3 = tl.load(in_ptr1 + (r2 + 16 * x3), xmask, other=0.0)
tmp13 = tl.load(in_ptr0 + (r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp14 = tl.load(in_ptr0 + (16 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp16 = tl.load(in_ptr0 + (32 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp18 = tl.load(in_ptr0 + (48 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp1 = 1.0
tmp2 = tmp0 - tmp1
tmp4 = -tmp3
tmp5 = libdevice.log1p(tmp4)
tmp6 = -100.0
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp2 * tmp7
tmp9 = tl_math.log(tmp3)
tmp10 = triton_helpers.maximum(tmp9, tmp6)
tmp11 = tmp0 * tmp10
tmp12 = tmp8 - tmp11
tmp15 = tmp13 + tmp14
tmp17 = tmp15 + tmp16
tmp19 = tmp17 + tmp18
tmp20 = 0.0
tmp21 = tmp19 > tmp20
tmp22 = tmp21.to(tl.float32)
tmp23 = tmp12 * tmp22
tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
tmp26 = tl.where(xmask, tmp24, 0)
tmp27 = tl.sum(tmp26, 1)[:, None]
tmp28 = tmp22 != tmp20
tmp29 = tmp28.to(tl.int64)
tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
tmp32 = tl.where(xmask, tmp30, 0)
tmp33 = tl.sum(tmp32, 1)[:, None]
tl.store(out_ptr0 + x3, tmp27, xmask)
tl.store(out_ptr1 + x3, tmp33, xmask)
@triton.jit
def triton_per_fused__to_copy_div_mean_1(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = tmp1.to(tl.float32)
tmp3 = tmp0 / tmp2
tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tmp6 = tl.sum(tmp4, 1)[:, None]
tmp7 = 16.0
tmp8 = tmp6 / tmp7
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)
def call(args):
arg0_1, arg1_1 = 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.int64)
get_raw_stream(0)
triton_per_fused__to_copy_binary_cross_entropy_gt_mul_ne_sum_0[grid(16)
](arg0_1, arg1_1, buf0, buf1, 16, 16, XBLOCK=8, 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__to_copy_div_mean_1[grid(1)](buf3, buf0, buf1, 1,
16, XBLOCK=1, num_warps=2, num_stages=1)
del buf0
del buf1
return buf3,
class PARALossNew(nn.Module):
"""
Softmax classifier for sentence-level relation extraction.
"""
def __init__(self):
"""
Args:
sentence_encoder: encoder for sentences
num_class: number of classes
id2rel: dictionary of id -> relation name mapping
"""
super().__init__()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
igorvlnascimento/redn
|
PARALoss
| false
| 15,598
|
[
"MIT"
] | 100
|
f40f19a0fdfbb11a7987996d520716a05bafd77b
|
https://github.com/igorvlnascimento/redn/tree/f40f19a0fdfbb11a7987996d520716a05bafd77b
|
Critic
|
import torch
import torch as t
import torch.nn as nn
class Critic(nn.Module):
def __init__(self, state_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim, 32)
self.fc2 = nn.Linear(32, 32)
self.fc3 = nn.Linear(32, 1)
def forward(self, state):
v = t.relu(self.fc1(state))
v = t.relu(self.fc2(v))
v = self.fc3(v)
return v
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 32
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, None)
tl.store(out_ptr0 + x2, tmp6, None)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (32, 4), (4, 1))
assert_size_stride(primals_2, (32,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (32, 32), (32, 1))
assert_size_stride(primals_5, (32,), (1,))
assert_size_stride(primals_6, (1, 32), (32, 1))
assert_size_stride(primals_7, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 32), (32, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 32), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 32), (512, 128, 32, 1), 0)
del buf0
buf7 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf1,
primals_2, buf7, 2048, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 32), (32, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 32), (32, 1), 0),
reinterpret_tensor(primals_4, (32, 32), (1, 32), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 32), (512, 128, 32, 1), 0)
del buf2
buf6 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf3,
primals_5, buf6, 2048, XBLOCK=256, num_warps=4, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 32),
(32, 1), 0), reinterpret_tensor(primals_6, (32, 1), (1, 32), 0),
alpha=1, beta=1, out=buf5)
del primals_7
return reinterpret_tensor(buf5, (4, 4, 4, 1), (16, 4, 1, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor(
buf3, (64, 32), (32, 1), 0), primals_6, buf6, primals_4, buf7
class CriticNew(nn.Module):
def __init__(self, state_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim, 32)
self.fc2 = nn.Linear(32, 32)
self.fc3 = nn.Linear(32, 1)
def forward(self, input_0):
primals_1 = self.fc1.weight
primals_2 = self.fc1.bias
primals_4 = self.fc2.weight
primals_5 = self.fc2.bias
primals_6 = self.fc3.weight
primals_7 = self.fc3.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
iffiX/machin
|
Critic
| false
| 15,599
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
DDPGActorCont
|
import torch
import torch as t
import torch.nn as nn
class DDPGActorCont(nn.Module):
def __init__(self, state_dim, action_dim, action_range):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_dim)
self.action_range = action_range
def forward(self, state):
a = t.relu(self.fc1(state))
a = t.relu(self.fc2(a))
a = t.tanh(self.fc3(a)) * self.action_range
return a
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_dim': 4, 'action_range': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_mul_tanh_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = libdevice.tanh(tmp0)
tmp2 = 4.0
tmp3 = tmp1 * tmp2
tl.store(out_ptr0 + x0, tmp3, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (16, 4), (4, 1))
assert_size_stride(primals_2, (16,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (16, 16), (16, 1))
assert_size_stride(primals_5, (16,), (1,))
assert_size_stride(primals_6, (4, 16), (16, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf0
buf7 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1,
primals_2, buf7, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 16), (16, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 16), (16, 1), 0),
reinterpret_tensor(primals_4, (16, 16), (1, 16), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 16), (256, 64, 16, 1), 0)
del buf2
buf6 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf3,
primals_5, buf6, 1024, 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, 16),
(16, 1), 0), reinterpret_tensor(primals_6, (16, 4), (1, 16), 0),
alpha=1, beta=1, out=buf4)
del primals_7
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_mul_tanh_1[grid(256)](buf4, buf5, 256, XBLOCK=256,
num_warps=4, num_stages=1)
return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 16), (16, 1), 0), reinterpret_tensor(
buf3, (64, 16), (16, 1), 0), buf4, primals_6, buf6, primals_4, buf7
class DDPGActorContNew(nn.Module):
def __init__(self, state_dim, action_dim, action_range):
super().__init__()
self.fc1 = nn.Linear(state_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, action_dim)
self.action_range = action_range
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]
|
iffiX/machin
|
DDPGActorCont
| false
| 15,600
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
MultiHeadAttention
|
import torch
import numpy as np
class MultiHeadAttention(torch.nn.Module):
def __init__(self, input_size, output_size, num_heads,
output_attentions=False):
super(MultiHeadAttention, self).__init__()
self.output_attentions = output_attentions
self.num_heads = num_heads
self.d_model_size = input_size
self.depth = int(output_size / self.num_heads)
self.Wq = torch.nn.Linear(input_size, output_size)
self.Wk = torch.nn.Linear(input_size, output_size)
def split_into_heads(self, x, batch_size):
x = x.reshape(batch_size, -1, self.num_heads, self.depth)
return x.permute([0, 2, 1, 3])
def forward(self, k, q):
batch_size = q.shape[0]
q = self.Wq(q)
k = self.Wk(k)
q = self.split_into_heads(q, batch_size)
k = self.split_into_heads(k, batch_size)
attn_score = torch.matmul(q, k.permute(0, 1, 3, 2))
attn_score = attn_score / np.sqrt(k.shape[-1])
return attn_score
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'output_size': 4, 'num_heads': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
assert_size_stride = torch._C._dynamo.guards.assert_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 = 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, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
del primals_2
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((4, 4, 16, 1), (64, 16, 1, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(16, 16)](buf0, primals_3, buf2, 16,
16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
del primals_3
buf3 = reinterpret_tensor(buf0, (4, 4, 1, 16), (64, 16, 16, 1), 0)
del buf0
triton_poi_fused_clone_0[grid(16, 16)](buf1, primals_5, buf3, 16,
16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
del buf1
del primals_5
buf4 = empty_strided_cuda((16, 16, 16), (256, 16, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf2, (16, 16, 1), (16, 1, 0),
0), reinterpret_tensor(buf3, (16, 1, 16), (16, 0, 1), 0), out=buf4)
return reinterpret_tensor(buf4, (4, 4, 16, 16), (1024, 256, 16, 1), 0
), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0
), reinterpret_tensor(buf2, (16, 1, 16), (16, 1, 1), 0
), reinterpret_tensor(buf3, (16, 16, 1), (16, 1, 16), 0)
class MultiHeadAttentionNew(torch.nn.Module):
def __init__(self, input_size, output_size, num_heads,
output_attentions=False):
super(MultiHeadAttentionNew, self).__init__()
self.output_attentions = output_attentions
self.num_heads = num_heads
self.d_model_size = input_size
self.depth = int(output_size / self.num_heads)
self.Wq = torch.nn.Linear(input_size, output_size)
self.Wk = torch.nn.Linear(input_size, output_size)
def split_into_heads(self, x, batch_size):
x = x.reshape(batch_size, -1, self.num_heads, self.depth)
return x.permute([0, 2, 1, 3])
def forward(self, input_0, input_1):
primals_2 = self.Wq.weight
primals_3 = self.Wq.bias
primals_4 = self.Wk.weight
primals_5 = self.Wk.bias
primals_1 = input_0
primals_6 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0]
|
igorvlnascimento/redn
|
MultiHeadAttention
| false
| 15,601
|
[
"MIT"
] | 100
|
f40f19a0fdfbb11a7987996d520716a05bafd77b
|
https://github.com/igorvlnascimento/redn/tree/f40f19a0fdfbb11a7987996d520716a05bafd77b
|
ConvD
|
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim
def normalization(planes, norm='gn'):
if norm == 'bn':
m = nn.BatchNorm3d(planes)
elif norm == 'gn':
m = nn.GroupNorm(4, planes)
elif norm == 'in':
m = nn.InstanceNorm3d(planes)
else:
raise ValueError('normalization type {} is not supported'.format(norm))
return m
class ConvD(nn.Module):
def __init__(self, inplanes, planes, dropout=0.0, norm='gn', first=False):
super(ConvD, self).__init__()
self.first = first
self.maxpool = nn.MaxPool3d(2, 2)
self.dropout = dropout
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv3d(inplanes, planes, 3, 1, 1, bias=False)
self.bn1 = normalization(planes, norm)
self.conv2 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn2 = normalization(planes, norm)
self.conv3 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn3 = normalization(planes, norm)
def forward(self, x):
if not self.first:
x = self.maxpool(x)
x = self.bn1(self.conv1(x))
y = self.relu(self.bn2(self.conv2(x)))
if self.dropout > 0:
y = F.dropout3d(y, self.dropout)
y = self.bn3(self.conv3(x))
return self.relu(x + y)
def get_inputs():
return [torch.rand([4, 8, 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.nn.parallel
import torch.optim
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_native_group_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
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_group_norm_1(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
x4 = xindex // 4
x1 = xindex // 4 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = tmp2 * tmp3
tmp6 = tmp4 * tmp5
tmp8 = tmp6 + tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_native_group_norm_relu_threshold_backward_2(in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, 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
x4 = xindex // 4
x1 = xindex // 4 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x3, xmask)
tmp2 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr5 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 - tmp2
tmp5 = tmp3 * tmp4
tmp7 = tmp5 * tmp6
tmp9 = tmp7 + tmp8
tmp10 = tmp0 + tmp9
tmp11 = tl.full([1], 0, tl.int32)
tmp12 = triton_helpers.maximum(tmp11, tmp10)
tmp13 = 0.0
tmp14 = tmp12 <= tmp13
tl.store(out_ptr0 + x3, tmp12, xmask)
tl.store(out_ptr1 + x3, tmp14, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10) = args
args.clear()
assert_size_stride(primals_1, (4, 8, 4, 4), (128, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4,), (1,))
assert_size_stride(primals_5, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_6, (4,), (1,))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_9, (4,), (1,))
assert_size_stride(primals_10, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = torch.ops.aten.max_pool3d_with_indices.default(primals_1, [2,
2, 2], [2, 2, 2])
del primals_1
buf1 = buf0[0]
del buf0
buf3 = extern_kernels.convolution(reinterpret_tensor(buf1, (1, 4, 4,
2, 2), (64, 16, 4, 2, 1), 0), primals_2, stride=(1, 1, 1),
padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf3, (1, 4, 4, 2, 2), (64, 16, 4, 2, 1))
buf4 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf5 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused_native_group_norm_0[grid(16)](buf3, buf4, buf5, 16,
XBLOCK=16, num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
triton_poi_fused_native_group_norm_1[grid(64)](buf3, buf4, buf5,
primals_3, primals_4, buf6, 64, XBLOCK=64, num_warps=1,
num_stages=1)
del primals_4
buf7 = extern_kernels.convolution(reinterpret_tensor(buf6, (1, 4, 4,
2, 2), (64, 16, 4, 2, 1), 0), primals_8, stride=(1, 1, 1),
padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf7, (1, 4, 4, 2, 2), (64, 16, 4, 2, 1))
buf8 = buf5
del buf5
buf9 = buf4
del buf4
triton_poi_fused_native_group_norm_0[grid(16)](buf7, buf8, buf9, 16,
XBLOCK=16, num_warps=1, num_stages=1)
buf10 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
buf11 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.bool)
triton_poi_fused_add_native_group_norm_relu_threshold_backward_2[grid
(64)](buf6, buf7, buf8, buf9, primals_9, primals_10, buf10,
buf11, 64, XBLOCK=64, num_warps=1, num_stages=1)
del buf8
del buf9
del primals_10
return (buf10, primals_2, primals_3, primals_8, primals_9,
reinterpret_tensor(buf1, (1, 4, 4, 2, 2), (64, 16, 4, 2, 1), 0),
buf3, reinterpret_tensor(buf6, (1, 4, 4, 2, 2), (64, 16, 4, 2, 1),
0), buf7, buf11)
def normalization(planes, norm='gn'):
if norm == 'bn':
m = nn.BatchNorm3d(planes)
elif norm == 'gn':
m = nn.GroupNorm(4, planes)
elif norm == 'in':
m = nn.InstanceNorm3d(planes)
else:
raise ValueError('normalization type {} is not supported'.format(norm))
return m
class ConvDNew(nn.Module):
def __init__(self, inplanes, planes, dropout=0.0, norm='gn', first=False):
super(ConvDNew, self).__init__()
self.first = first
self.maxpool = nn.MaxPool3d(2, 2)
self.dropout = dropout
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv3d(inplanes, planes, 3, 1, 1, bias=False)
self.bn1 = normalization(planes, norm)
self.conv2 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn2 = normalization(planes, norm)
self.conv3 = nn.Conv3d(planes, planes, 3, 1, 1, bias=False)
self.bn3 = normalization(planes, norm)
def forward(self, input_0):
primals_2 = self.conv1.weight
primals_3 = self.bn1.weight
primals_4 = self.bn1.bias
primals_5 = self.conv2.weight
primals_6 = self.bn2.weight
primals_7 = self.bn2.bias
primals_8 = self.conv3.weight
primals_9 = self.bn3.weight
primals_10 = self.bn3.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])
return output[0]
|
ieee820/BraTS2018-tumor-segmentation
|
ConvD
| false
| 15,602
|
[
"MIT"
] | 157
|
22e1a22909a0c21503b5ef5fc6860a1e1131e851
|
https://github.com/ieee820/BraTS2018-tumor-segmentation/tree/22e1a22909a0c21503b5ef5fc6860a1e1131e851
|
DDPGCritic
|
import torch
import torch as t
import torch.nn as nn
class DDPGCritic(nn.Module):
def __init__(self, state_dim, action_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim + action_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, 1)
def forward(self, state, action):
state_action = t.cat([state, action], 1)
q = t.relu(self.fc1(state_action))
q = t.relu(self.fc2(q))
q = self.fc3(q)
return q
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'state_dim': 4, 'action_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 32
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 8
x1 = xindex // 8
x2 = xindex
tmp0 = x0
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp6 = tmp0 >= tmp3
tl.full([1], 8, tl.int64)
tmp9 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp6 & xmask,
eviction_policy='evict_last', other=0.0)
tmp10 = tl.where(tmp4, tmp5, tmp9)
tl.store(out_ptr0 + x2, tmp10, xmask)
@triton.jit
def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (16, 8), (8, 1))
assert_size_stride(primals_4, (16,), (1,))
assert_size_stride(primals_5, (16, 16), (16, 1))
assert_size_stride(primals_6, (16,), (1,))
assert_size_stride(primals_7, (1, 16), (16, 1))
assert_size_stride(primals_8, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(32)](primals_1, primals_2, buf0, 32,
XBLOCK=32, num_warps=1, num_stages=1)
del primals_1
del primals_2
buf1 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (8, 16), (1,
8), 0), out=buf1)
del primals_3
buf2 = buf1
del buf1
triton_poi_fused_relu_1[grid(64)](buf2, primals_4, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_4
buf3 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
extern_kernels.mm(buf2, reinterpret_tensor(primals_5, (16, 16), (1,
16), 0), out=buf3)
buf4 = buf3
del buf3
triton_poi_fused_relu_1[grid(64)](buf4, primals_6, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_6
buf6 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_8, buf4, reinterpret_tensor(primals_7,
(16, 1), (1, 16), 0), alpha=1, beta=1, out=buf6)
del primals_8
return buf6, buf0, buf2, buf4, primals_7, primals_5
class DDPGCriticNew(nn.Module):
def __init__(self, state_dim, action_dim):
super().__init__()
self.fc1 = nn.Linear(state_dim + action_dim, 16)
self.fc2 = nn.Linear(16, 16)
self.fc3 = nn.Linear(16, 1)
def forward(self, input_0, input_1):
primals_3 = self.fc1.weight
primals_4 = self.fc1.bias
primals_5 = self.fc2.weight
primals_6 = self.fc2.bias
primals_7 = self.fc3.weight
primals_8 = self.fc3.bias
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8])
return output[0]
|
iffiX/machin
|
DDPGCritic
| false
| 15,603
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
RNN
|
import torch
import torch.nn as nn
from torch.autograd import Variable
class RNN(nn.Module):
def __init__(self, category_size, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.category_size = category_size
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.i2h = nn.Linear(category_size + input_size + hidden_size,
hidden_size)
self.i2o = nn.Linear(category_size + input_size + hidden_size,
output_size)
self.o2o = nn.Linear(hidden_size + output_size, output_size)
self.softmax = nn.LogSoftmax()
def forward(self, category, input, hidden):
input_combined = torch.cat((category, input, hidden), 1)
hidden = self.i2h(input_combined)
output = self.i2o(input_combined)
output_combined = torch.cat((hidden, output), 1)
output = self.o2o(output_combined)
return output, hidden
def init_hidden(self):
return Variable(torch.zeros(1, self.hidden_size))
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'category_size': 4, 'input_size': 4, 'hidden_size': 4,
'output_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
from torch.autograd import Variable
assert_size_stride = torch._C._dynamo.guards.assert_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 = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 12
x1 = xindex // 12
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
tmp7 = tl.full([1], 8, tl.int64)
tmp8 = tmp0 < tmp7
tmp9 = tmp6 & tmp8
tmp10 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp9 & xmask,
eviction_policy='evict_last', other=0.0)
tmp11 = tmp0 >= tmp7
tl.full([1], 12, tl.int64)
tmp14 = tl.load(in_ptr2 + (4 * x1 + (-8 + x0)), tmp11 & xmask,
eviction_policy='evict_last', other=0.0)
tmp15 = tl.where(tmp9, tmp10, tmp14)
tmp16 = tl.where(tmp4, tmp5, tmp15)
tl.store(out_ptr0 + x2, tmp16, xmask)
@triton.jit
def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tl.store(out_ptr0 + (x0 + 8 * x1), tmp0, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 12), (12, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 12), (12, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 8), (8, 1))
assert_size_stride(primals_9, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(48)](primals_1, primals_2, primals_3,
buf0, 48, XBLOCK=64, num_warps=1, num_stages=1)
del primals_1
del primals_2
del primals_3
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, buf0, reinterpret_tensor(primals_4,
(12, 4), (1, 12), 0), alpha=1, beta=1, out=buf1)
del primals_4
del primals_5
buf4 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
buf2 = reinterpret_tensor(buf4, (4, 4), (8, 1), 4)
extern_kernels.addmm(primals_7, buf0, reinterpret_tensor(primals_6,
(12, 4), (1, 12), 0), alpha=1, beta=1, out=buf2)
del primals_6
del primals_7
buf3 = reinterpret_tensor(buf4, (4, 4), (8, 1), 0)
triton_poi_fused_cat_1[grid(16)](buf1, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_9, buf4, reinterpret_tensor(primals_8,
(8, 4), (1, 8), 0), alpha=1, beta=1, out=buf5)
del primals_9
return buf5, buf1, buf0, buf4, primals_8
class RNNNew(nn.Module):
def __init__(self, category_size, input_size, hidden_size, output_size):
super(RNNNew, self).__init__()
self.category_size = category_size
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.i2h = nn.Linear(category_size + input_size + hidden_size,
hidden_size)
self.i2o = nn.Linear(category_size + input_size + hidden_size,
output_size)
self.o2o = nn.Linear(hidden_size + output_size, output_size)
self.softmax = nn.LogSoftmax()
def init_hidden(self):
return Variable(torch.zeros(1, self.hidden_size))
def forward(self, input_0, input_1, input_2):
primals_4 = self.i2h.weight
primals_5 = self.i2h.bias
primals_6 = self.i2o.weight
primals_7 = self.i2o.bias
primals_8 = self.o2o.weight
primals_9 = self.o2o.bias
primals_1 = input_0
primals_2 = input_1
primals_3 = input_2
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9])
return output[0], output[1]
|
igorwood/practical-pytorch
|
RNN
| false
| 15,604
|
[
"MIT"
] | 4,847
|
c08fc28ba1f7d6838c3938076cc1b03d90dccace
|
https://github.com/igorwood/practical-pytorch/tree/c08fc28ba1f7d6838c3938076cc1b03d90dccace
|
ConvTanh
|
import torch
import numpy as np
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4,
'stride': 1}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
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):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 8
x1 = xindex // 8 % 8
x2 = xindex // 64
x3 = xindex
tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-2 +
x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-2 + x1)) + 16 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_convolution_div_mul_tanh_1(in_out_ptr0, 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
x3 = xindex
x1 = xindex // 25 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.00392156862745098
tmp4 = tmp2 * tmp3
tmp5 = libdevice.tanh(tmp4)
tmp6 = 150.0
tmp7 = tmp5 * tmp6
tmp8 = 127.5
tmp9 = tmp7 + tmp8
tl.store(in_out_ptr0 + x3, tmp2, xmask)
tl.store(out_ptr0 + x3, tmp9, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_reflection_pad2d_0[grid(1024)](primals_1, buf0,
1024, XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 5, 5), (100, 25, 5, 1))
buf2 = buf1
del buf1
buf3 = empty_strided_cuda((4, 4, 5, 5), (100, 25, 5, 1), torch.float32)
triton_poi_fused_add_convolution_div_mul_tanh_1[grid(400)](buf2,
primals_3, buf3, 400, XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
return buf3, primals_2, buf0, buf2
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanhNew(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanhNew, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, input_0):
primals_1 = self.conv2d.weight
primals_3 = self.conv2d.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
irsisyphus/reconet
|
ConvTanh
| false
| 15,605
|
[
"MIT"
] | 56
|
863acf8dde4d45c8521634af27878fe04f3b2e56
|
https://github.com/irsisyphus/reconet/tree/863acf8dde4d45c8521634af27878fe04f3b2e56
|
BertSelfAttention
|
from _paritybench_helpers import _mock_config
import math
import torch
from torch import nn
class BertSelfAttention(nn.Module):
def __init__(self, config):
super(BertSelfAttention, self).__init__()
if config.hidden_size % config.num_attention_heads != 0:
raise ValueError(
'The hidden size (%d) is not a multiple of the number of attention heads (%d)'
% (config.hidden_size, config.num_attention_heads))
self.num_attention_heads = config.num_attention_heads
self.attention_head_size = int(config.hidden_size / config.
num_attention_heads)
self.all_head_size = (self.num_attention_heads * self.
attention_head_size)
self.query = nn.Linear(config.hidden_size, self.all_head_size)
self.key = nn.Linear(config.hidden_size, self.all_head_size)
self.value = nn.Linear(config.hidden_size, self.all_head_size)
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
def transpose_for_scores(self, x):
new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.
attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def forward(self, hidden_states, attention_mask, join_mask=None,
only_cls_output=False):
global last_attn_output
if only_cls_output:
mixed_query_layer = self.query(hidden_states[:, :1])
else:
mixed_query_layer = self.query(hidden_states)
query_layer = self.transpose_for_scores(mixed_query_layer)
mixed_key_layer = self.key(hidden_states)
key_layer = self.transpose_for_scores(mixed_key_layer)
mixed_value_layer = self.value(hidden_states)
value_layer = self.transpose_for_scores(mixed_value_layer)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1,
-2))
attention_scores = attention_scores / math.sqrt(self.
attention_head_size)
attention_scores = attention_scores + attention_mask
if join_mask is not None:
attention_scores = attention_scores + join_mask
attention_probs = nn.Softmax(dim=-1)(attention_scores)
last_attn_output = attention_probs
attention_probs = self.dropout(attention_probs)
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.
all_head_size,)
context_layer = context_layer.view(*new_context_layer_shape)
return context_layer
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'config': _mock_config(hidden_size=4, num_attention_heads=
4, attention_probs_dropout_prob=0.5)}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)
@triton.jit
def triton_poi_fused__softmax_add_div_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 16
tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp15 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp17 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp5 * tmp1
tmp8 = tmp6 + tmp7
tmp9 = triton_helpers.maximum(tmp4, tmp8)
tmp11 = tmp10 * tmp1
tmp13 = tmp11 + tmp12
tmp14 = triton_helpers.maximum(tmp9, tmp13)
tmp16 = tmp15 * tmp1
tmp18 = tmp16 + tmp17
tmp19 = triton_helpers.maximum(tmp14, tmp18)
tmp20 = tmp4 - tmp19
tmp21 = tl_math.exp(tmp20)
tmp22 = tmp8 - tmp19
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp21 + tmp23
tmp25 = tmp13 - tmp19
tmp26 = tl_math.exp(tmp25)
tmp27 = tmp24 + tmp26
tmp28 = tmp18 - tmp19
tmp29 = tl_math.exp(tmp28)
tmp30 = tmp27 + tmp29
tl.store(out_ptr0 + x2, tmp19, xmask)
tl.store(out_ptr1 + x2, tmp30, xmask)
@triton.jit
def triton_poi_fused__softmax_add_div_2(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
x4 = xindex % 64
x5 = xindex // 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp3 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last')
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 - tmp5
tmp7 = tl_math.exp(tmp6)
tmp9 = tmp7 / tmp8
tl.store(in_out_ptr0 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4), (4, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2)
del primals_6
buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(16, 4)](buf0, primals_2, buf3, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_2
buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0)
del buf0
triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5)
buf6 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0)
del buf1
buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
triton_poi_fused__softmax_add_div_1[grid(64)](buf5, primals_8, buf6,
buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf8 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf5
triton_poi_fused__softmax_add_div_2[grid(256)](buf8, primals_8,
buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_8
buf9 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf7
triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf9, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_7
buf10 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0)
del buf2
extern_kernels.bmm(reinterpret_tensor(buf8, (16, 4, 4), (16, 4, 1),
0), reinterpret_tensor(buf9, (16, 4, 1), (4, 1, 0), 0), out=buf10)
buf11 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf6
triton_poi_fused_clone_3[grid(16, 4)](buf10, buf11, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
del buf10
return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0
), buf8, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0
), buf8, reinterpret_tensor(buf9, (16, 1, 4), (4, 1, 1), 0
), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0
), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0)
class BertSelfAttentionNew(nn.Module):
def __init__(self, config):
super(BertSelfAttentionNew, self).__init__()
if config.hidden_size % config.num_attention_heads != 0:
raise ValueError(
'The hidden size (%d) is not a multiple of the number of attention heads (%d)'
% (config.hidden_size, config.num_attention_heads))
self.num_attention_heads = config.num_attention_heads
self.attention_head_size = int(config.hidden_size / config.
num_attention_heads)
self.all_head_size = (self.num_attention_heads * self.
attention_head_size)
self.query = nn.Linear(config.hidden_size, self.all_head_size)
self.key = nn.Linear(config.hidden_size, self.all_head_size)
self.value = nn.Linear(config.hidden_size, self.all_head_size)
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
def transpose_for_scores(self, x):
new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.
attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def forward(self, input_0, input_1):
primals_1 = self.query.weight
primals_2 = self.query.bias
primals_4 = self.key.weight
primals_5 = self.key.bias
primals_6 = self.value.weight
primals_7 = self.value.bias
primals_3 = input_0
primals_8 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8])
return output[0]
|
Georgetown-IR-Lab/OpenNIR
|
BertSelfAttention
| false
| 15,606
|
[
"MIT"
] | 140
|
7d93e8643fe311e3e9c7a0678efe9775fd80485e
|
https://github.com/Georgetown-IR-Lab/OpenNIR/tree/7d93e8643fe311e3e9c7a0678efe9775fd80485e
|
EncoderLayer
|
import math
import torch
from torch import nn
class LayerNorm(nn.Module):
def __init__(self, d_model, eps=1e-12):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(d_model))
self.beta = nn.Parameter(torch.zeros(d_model))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
out = (x - mean) / (std + self.eps)
out = self.gamma * out + self.beta
return out
class ScaleDotProductAttention(nn.Module):
"""
compute scale dot product attention
Query : given sentence that we focused on (decoder)
Key : every sentence to check relationship with Qeury(encoder)
Value : every sentence same with Key (encoder)
"""
def __init__(self):
super(ScaleDotProductAttention, self).__init__()
self.softmax = nn.Softmax(dim=-1)
def forward(self, q, k, v, mask=None, e=1e-12):
_batch_size, _head, _length, d_tensor = k.size()
k_t = k.transpose(2, 3)
score = q @ k_t / math.sqrt(d_tensor)
if mask is not None:
score = score.masked_fill(mask == 0, -e)
score = self.softmax(score)
v = score @ v
return v, score
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_head):
super(MultiHeadAttention, self).__init__()
self.n_head = n_head
self.attention = ScaleDotProductAttention()
self.w_q = nn.Linear(d_model, d_model)
self.w_k = nn.Linear(d_model, d_model)
self.w_v = nn.Linear(d_model, d_model)
self.w_concat = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
q, k, v = self.w_q(q), self.w_k(k), self.w_v(v)
q, k, v = self.split(q), self.split(k), self.split(v)
out, _attention = self.attention(q, k, v, mask=mask)
out = self.concat(out)
out = self.w_concat(out)
return out
def split(self, tensor):
"""
split tensor by number of head
:param tensor: [batch_size, length, d_model]
:return: [batch_size, head, length, d_tensor]
"""
batch_size, length, d_model = tensor.size()
d_tensor = d_model // self.n_head
tensor = tensor.view(batch_size, length, self.n_head, d_tensor
).transpose(1, 2)
return tensor
def concat(self, tensor):
"""
inverse function of self.split(tensor : torch.Tensor)
:param tensor: [batch_size, head, length, d_tensor]
:return: [batch_size, length, d_model]
"""
batch_size, head, length, d_tensor = tensor.size()
d_model = head * d_tensor
tensor = tensor.transpose(1, 2).contiguous().view(batch_size,
length, d_model)
return tensor
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, hidden, drop_prob=0.1):
super(PositionwiseFeedForward, self).__init__()
self.linear1 = nn.Linear(d_model, hidden)
self.linear2 = nn.Linear(hidden, d_model)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=drop_prob)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linear2(x)
return x
class EncoderLayer(nn.Module):
def __init__(self, d_model, ffn_hidden, n_head, drop_prob):
super(EncoderLayer, self).__init__()
self.attention = MultiHeadAttention(d_model=d_model, n_head=n_head)
self.norm1 = LayerNorm(d_model=d_model)
self.dropout1 = nn.Dropout(p=drop_prob)
self.ffn = PositionwiseFeedForward(d_model=d_model, hidden=
ffn_hidden, drop_prob=drop_prob)
self.norm2 = LayerNorm(d_model=d_model)
self.dropout2 = nn.Dropout(p=drop_prob)
def forward(self, x, s_mask):
_x = x
x = self.attention(q=x, k=x, v=x, mask=s_mask)
x = self.norm1(x + _x)
x = self.dropout1(x)
_x = x
x = self.ffn(x)
x = self.norm2(x + _x)
x = self.dropout2(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'d_model': 4, 'ffn_hidden': 4, 'n_head': 4, 'drop_prob': 0.5}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)
@triton.jit
def triton_poi_fused_eq_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.0
tmp2 = tmp0 == tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused__softmax_div_masked_fill_2(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
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')
tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp16 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp17 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = 1.0
tmp3 = tmp1 * tmp2
tmp4 = -9.999999960041972e-13
tmp5 = tl.where(tmp0, tmp4, tmp3)
tmp8 = tmp7 * tmp2
tmp9 = tl.where(tmp6, tmp4, tmp8)
tmp10 = triton_helpers.maximum(tmp5, tmp9)
tmp13 = tmp12 * tmp2
tmp14 = tl.where(tmp11, tmp4, tmp13)
tmp15 = triton_helpers.maximum(tmp10, tmp14)
tmp18 = tmp17 * tmp2
tmp19 = tl.where(tmp16, tmp4, tmp18)
tmp20 = triton_helpers.maximum(tmp15, tmp19)
tmp21 = tmp5 - tmp20
tmp22 = tl_math.exp(tmp21)
tmp23 = tmp9 - tmp20
tmp24 = tl_math.exp(tmp23)
tmp25 = tmp22 + tmp24
tmp26 = tmp14 - tmp20
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp25 + tmp27
tmp29 = tmp19 - tmp20
tmp30 = tl_math.exp(tmp29)
tmp31 = tmp28 + tmp30
tl.store(out_ptr0 + x0, tmp20, xmask)
tl.store(out_ptr1 + x0, tmp31, xmask)
@triton.jit
def triton_poi_fused__softmax_div_masked_fill_3(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask).to(tl.int1)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp6 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp2 = 1.0
tmp3 = tmp1 * tmp2
tmp4 = -9.999999960041972e-13
tmp5 = tl.where(tmp0, tmp4, tmp3)
tmp7 = tmp5 - tmp6
tmp8 = tl_math.exp(tmp7)
tmp10 = tmp8 / tmp9
tl.store(in_out_ptr0 + x2, tmp10, xmask)
@triton.jit
def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_add_mean_std_5(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 + tmp8
tmp10 = tmp6 + tmp9
tmp13 = tmp11 + tmp12
tmp14 = tmp10 + tmp13
tmp15 = 4.0
tmp16 = tmp14 / tmp15
tmp17 = tmp2 - tmp16
tmp18 = tmp17 * tmp17
tmp19 = tmp5 - tmp16
tmp20 = tmp19 * tmp19
tmp21 = tmp18 + tmp20
tmp22 = tmp9 - tmp16
tmp23 = tmp22 * tmp22
tmp24 = tmp21 + tmp23
tmp25 = tmp13 - tmp16
tmp26 = tmp25 * tmp25
tmp27 = tmp24 + tmp26
tmp28 = 3.0
tmp29 = tmp27 / tmp28
tl.store(in_out_ptr0 + x0, tmp29, xmask)
tl.store(out_ptr0 + x0, tmp16, xmask)
@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr2 + x2, xmask)
tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 - tmp4
tmp7 = libdevice.sqrt(tmp6)
tmp8 = 1e-12
tmp9 = tmp7 + tmp8
tmp10 = tmp5 / tmp9
tmp11 = tmp0 * tmp10
tmp13 = tmp11 + tmp12
tl.store(out_ptr0 + x2, tmp13, xmask)
@triton.jit
def triton_poi_fused_relu_threshold_backward_7(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
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_8(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_9(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 4.0
tmp10 = tmp8 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = 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-12
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, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17, primals_18
) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4), (4, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_9, (4, 4), (4, 1))
assert_size_stride(primals_10, (4,), (1,))
assert_size_stride(primals_11, (4,), (1,))
assert_size_stride(primals_12, (4,), (1,))
assert_size_stride(primals_13, (4, 4), (4, 1))
assert_size_stride(primals_14, (4,), (1,))
assert_size_stride(primals_15, (4, 4), (4, 1))
assert_size_stride(primals_16, (4,), (1,))
assert_size_stride(primals_17, (4,), (1,))
assert_size_stride(primals_18, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
del primals_2
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2)
del primals_6
buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(16, 4)](buf0, primals_3, buf3, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_3
buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0)
del buf0
triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5)
buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
triton_poi_fused_eq_1[grid(256)](primals_8, buf6, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_8
buf7 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0)
del buf1
buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf6, buf5,
buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf5
triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf9, buf6,
buf7, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1)
buf10 = reinterpret_tensor(buf8, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf8
triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf10, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_7
buf11 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0)
del buf2
extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1),
0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11)
buf12 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf7
triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0)
del buf11
extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4),
(4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0),
alpha=1, beta=1, out=buf13)
del primals_10
buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
buf15 = buf14
del buf14
buf16 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
triton_poi_fused_add_mean_std_5[grid(16)](buf15, buf13, primals_1,
buf16, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_div_mean_mul_std_sub_6[grid(64)](primals_11,
buf13, primals_1, buf16, buf15, primals_12, buf17, 64, XBLOCK=
64, num_warps=1, num_stages=1)
del buf15
del buf16
del primals_12
buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf17, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_13, (4, 4), (1, 4), 0), out=buf18)
buf19 = reinterpret_tensor(buf18, (4, 4, 4), (16, 4, 1), 0)
del buf18
buf23 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_7[grid(64)](buf19,
primals_14, buf23, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_14
buf20 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf19, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_15, (4, 4), (1, 4), 0), out=buf20)
buf21 = reinterpret_tensor(buf20, (4, 4, 4), (16, 4, 1), 0)
del buf20
triton_poi_fused_add_8[grid(64)](buf21, primals_16, buf17, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_16
buf22 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_div_mean_mul_std_sub_9[grid(64)](primals_17,
buf21, primals_18, buf22, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_18
return (buf22, primals_1, primals_11, primals_17, buf6, buf9,
reinterpret_tensor(buf12, (16, 4), (4, 1), 0), buf13,
reinterpret_tensor(buf17, (16, 4), (4, 1), 0), reinterpret_tensor(
buf19, (16, 4), (4, 1), 0), buf21, primals_15, buf23, primals_13,
primals_9, reinterpret_tensor(buf10, (16, 1, 4), (4, 1, 1), 0),
reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0),
reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0))
class LayerNorm(nn.Module):
def __init__(self, d_model, eps=1e-12):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(d_model))
self.beta = nn.Parameter(torch.zeros(d_model))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
out = (x - mean) / (std + self.eps)
out = self.gamma * out + self.beta
return out
class ScaleDotProductAttention(nn.Module):
"""
compute scale dot product attention
Query : given sentence that we focused on (decoder)
Key : every sentence to check relationship with Qeury(encoder)
Value : every sentence same with Key (encoder)
"""
def __init__(self):
super(ScaleDotProductAttention, self).__init__()
self.softmax = nn.Softmax(dim=-1)
def forward(self, q, k, v, mask=None, e=1e-12):
_batch_size, _head, _length, d_tensor = k.size()
k_t = k.transpose(2, 3)
score = q @ k_t / math.sqrt(d_tensor)
if mask is not None:
score = score.masked_fill(mask == 0, -e)
score = self.softmax(score)
v = score @ v
return v, score
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_head):
super(MultiHeadAttention, self).__init__()
self.n_head = n_head
self.attention = ScaleDotProductAttention()
self.w_q = nn.Linear(d_model, d_model)
self.w_k = nn.Linear(d_model, d_model)
self.w_v = nn.Linear(d_model, d_model)
self.w_concat = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
q, k, v = self.w_q(q), self.w_k(k), self.w_v(v)
q, k, v = self.split(q), self.split(k), self.split(v)
out, _attention = self.attention(q, k, v, mask=mask)
out = self.concat(out)
out = self.w_concat(out)
return out
def split(self, tensor):
"""
split tensor by number of head
:param tensor: [batch_size, length, d_model]
:return: [batch_size, head, length, d_tensor]
"""
batch_size, length, d_model = tensor.size()
d_tensor = d_model // self.n_head
tensor = tensor.view(batch_size, length, self.n_head, d_tensor
).transpose(1, 2)
return tensor
def concat(self, tensor):
"""
inverse function of self.split(tensor : torch.Tensor)
:param tensor: [batch_size, head, length, d_tensor]
:return: [batch_size, length, d_model]
"""
batch_size, head, length, d_tensor = tensor.size()
d_model = head * d_tensor
tensor = tensor.transpose(1, 2).contiguous().view(batch_size,
length, d_model)
return tensor
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, hidden, drop_prob=0.1):
super(PositionwiseFeedForward, self).__init__()
self.linear1 = nn.Linear(d_model, hidden)
self.linear2 = nn.Linear(hidden, d_model)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=drop_prob)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linear2(x)
return x
class EncoderLayerNew(nn.Module):
def __init__(self, d_model, ffn_hidden, n_head, drop_prob):
super(EncoderLayerNew, self).__init__()
self.attention = MultiHeadAttention(d_model=d_model, n_head=n_head)
self.norm1 = LayerNorm(d_model=d_model)
self.dropout1 = nn.Dropout(p=drop_prob)
self.ffn = PositionwiseFeedForward(d_model=d_model, hidden=
ffn_hidden, drop_prob=drop_prob)
self.norm2 = LayerNorm(d_model=d_model)
self.dropout2 = nn.Dropout(p=drop_prob)
def forward(self, input_0, input_1):
primals_2 = self.attention.w_q.weight
primals_3 = self.attention.w_q.bias
primals_4 = self.attention.w_k.weight
primals_5 = self.attention.w_k.bias
primals_6 = self.attention.w_v.weight
primals_7 = self.attention.w_v.bias
primals_9 = self.attention.w_concat.weight
primals_10 = self.attention.w_concat.bias
primals_11 = self.norm1.gamma
primals_12 = self.norm1.beta
primals_13 = self.ffn.linear1.weight
primals_14 = self.ffn.linear1.bias
primals_15 = self.ffn.linear2.weight
primals_16 = self.ffn.linear2.bias
primals_17 = self.norm2.gamma
primals_18 = self.norm2.beta
primals_1 = input_0
primals_8 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18])
return output[0]
|
hyunwoongko/transformer
|
EncoderLayer
| false
| 15,607
|
[
"Apache-2.0"
] | 233
|
8f7aaa19d37b088c156db0512868127ba9bf1a0f
|
https://github.com/hyunwoongko/transformer/tree/8f7aaa19d37b088c156db0512868127ba9bf1a0f
|
LogTaylorSoftmaxV1
|
import torch
import torch.nn as nn
def taylor_softmax_v1(x, dim=1, n=4, use_log=False):
assert n % 2 == 0 and n > 0
fn = torch.ones_like(x)
denor = 1.0
for i in range(1, n + 1):
denor *= i
fn = fn + x.pow(i) / denor
out = fn / fn.sum(dim=dim, keepdims=True)
if use_log:
out = out.log()
return out
class LogTaylorSoftmaxV1(nn.Module):
def __init__(self, dim=1, n=2):
super(LogTaylorSoftmaxV1, self).__init__()
assert n % 2 == 0
self.dim = dim
self.n = n
def forward(self, x):
return taylor_softmax_v1(x, self.dim, self.n, use_log=True)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_div_log_ones_like_pow_sum_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
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp8 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp14 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp21 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp28 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tmp3 = tmp1 + tmp2
tmp4 = tmp0 * tmp0
tmp5 = 0.5
tmp6 = tmp4 * tmp5
tmp7 = tmp3 + tmp6
tmp9 = tmp8 * tmp1
tmp10 = tmp1 + tmp9
tmp11 = tmp8 * tmp8
tmp12 = tmp11 * tmp5
tmp13 = tmp10 + tmp12
tmp15 = tmp14 * tmp1
tmp16 = tmp1 + tmp15
tmp17 = tmp14 * tmp14
tmp18 = tmp17 * tmp5
tmp19 = tmp16 + tmp18
tmp20 = tmp13 + tmp19
tmp22 = tmp21 * tmp1
tmp23 = tmp1 + tmp22
tmp24 = tmp21 * tmp21
tmp25 = tmp24 * tmp5
tmp26 = tmp23 + tmp25
tmp27 = tmp20 + tmp26
tmp29 = tmp28 * tmp1
tmp30 = tmp1 + tmp29
tmp31 = tmp28 * tmp28
tmp32 = tmp31 * tmp5
tmp33 = tmp30 + tmp32
tmp34 = tmp27 + tmp33
tmp35 = tmp7 / tmp34
tmp36 = tl_math.log(tmp35)
tl.store(in_out_ptr0 + x3, tmp36, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_add_div_log_ones_like_pow_sum_0[grid(256)](buf1,
arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1)
del arg0_1
return buf1,
def taylor_softmax_v1(x, dim=1, n=4, use_log=False):
assert n % 2 == 0 and n > 0
fn = torch.ones_like(x)
denor = 1.0
for i in range(1, n + 1):
denor *= i
fn = fn + x.pow(i) / denor
out = fn / fn.sum(dim=dim, keepdims=True)
if use_log:
out = out.log()
return out
class LogTaylorSoftmaxV1New(nn.Module):
def __init__(self, dim=1, n=2):
super(LogTaylorSoftmaxV1New, self).__init__()
assert n % 2 == 0
self.dim = dim
self.n = n
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ishine/DeepKE
|
LogTaylorSoftmaxV1
| false
| 15,608
|
[
"MIT"
] | 676
|
75bcfb3e045bb2197ac5c0847693c2a647f76576
|
https://github.com/ishine/DeepKE/tree/75bcfb3e045bb2197ac5c0847693c2a647f76576
|
DecoderLayer
|
import math
import torch
from torch import nn
class LayerNorm(nn.Module):
def __init__(self, d_model, eps=1e-12):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(d_model))
self.beta = nn.Parameter(torch.zeros(d_model))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
out = (x - mean) / (std + self.eps)
out = self.gamma * out + self.beta
return out
class ScaleDotProductAttention(nn.Module):
"""
compute scale dot product attention
Query : given sentence that we focused on (decoder)
Key : every sentence to check relationship with Qeury(encoder)
Value : every sentence same with Key (encoder)
"""
def __init__(self):
super(ScaleDotProductAttention, self).__init__()
self.softmax = nn.Softmax(dim=-1)
def forward(self, q, k, v, mask=None, e=1e-12):
_batch_size, _head, _length, d_tensor = k.size()
k_t = k.transpose(2, 3)
score = q @ k_t / math.sqrt(d_tensor)
if mask is not None:
score = score.masked_fill(mask == 0, -e)
score = self.softmax(score)
v = score @ v
return v, score
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_head):
super(MultiHeadAttention, self).__init__()
self.n_head = n_head
self.attention = ScaleDotProductAttention()
self.w_q = nn.Linear(d_model, d_model)
self.w_k = nn.Linear(d_model, d_model)
self.w_v = nn.Linear(d_model, d_model)
self.w_concat = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
q, k, v = self.w_q(q), self.w_k(k), self.w_v(v)
q, k, v = self.split(q), self.split(k), self.split(v)
out, _attention = self.attention(q, k, v, mask=mask)
out = self.concat(out)
out = self.w_concat(out)
return out
def split(self, tensor):
"""
split tensor by number of head
:param tensor: [batch_size, length, d_model]
:return: [batch_size, head, length, d_tensor]
"""
batch_size, length, d_model = tensor.size()
d_tensor = d_model // self.n_head
tensor = tensor.view(batch_size, length, self.n_head, d_tensor
).transpose(1, 2)
return tensor
def concat(self, tensor):
"""
inverse function of self.split(tensor : torch.Tensor)
:param tensor: [batch_size, head, length, d_tensor]
:return: [batch_size, length, d_model]
"""
batch_size, head, length, d_tensor = tensor.size()
d_model = head * d_tensor
tensor = tensor.transpose(1, 2).contiguous().view(batch_size,
length, d_model)
return tensor
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, hidden, drop_prob=0.1):
super(PositionwiseFeedForward, self).__init__()
self.linear1 = nn.Linear(d_model, hidden)
self.linear2 = nn.Linear(hidden, d_model)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=drop_prob)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linear2(x)
return x
class DecoderLayer(nn.Module):
def __init__(self, d_model, ffn_hidden, n_head, drop_prob):
super(DecoderLayer, self).__init__()
self.self_attention = MultiHeadAttention(d_model=d_model, n_head=n_head
)
self.norm1 = LayerNorm(d_model=d_model)
self.dropout1 = nn.Dropout(p=drop_prob)
self.enc_dec_attention = MultiHeadAttention(d_model=d_model, n_head
=n_head)
self.norm2 = LayerNorm(d_model=d_model)
self.dropout2 = nn.Dropout(p=drop_prob)
self.ffn = PositionwiseFeedForward(d_model=d_model, hidden=
ffn_hidden, drop_prob=drop_prob)
self.norm3 = LayerNorm(d_model=d_model)
self.dropout3 = nn.Dropout(p=drop_prob)
def forward(self, dec, enc, t_mask, s_mask):
_x = dec
x = self.self_attention(q=dec, k=dec, v=dec, mask=t_mask)
x = self.norm1(x + _x)
x = self.dropout1(x)
if enc is not None:
_x = x
x = self.enc_dec_attention(q=x, k=enc, v=enc, mask=s_mask)
x = self.norm2(x + _x)
x = self.dropout2(x)
_x = x
x = self.ffn(x)
x = self.norm3(x + _x)
x = self.dropout3(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4,
4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'d_model': 4, 'ffn_hidden': 4, 'n_head': 4, 'drop_prob': 0.5}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)
@triton.jit
def triton_poi_fused_eq_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.0
tmp2 = tmp0 == tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused__softmax_div_masked_fill_2(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
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')
tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp16 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
).to(tl.int1)
tmp17 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = 1.0
tmp3 = tmp1 * tmp2
tmp4 = -9.999999960041972e-13
tmp5 = tl.where(tmp0, tmp4, tmp3)
tmp8 = tmp7 * tmp2
tmp9 = tl.where(tmp6, tmp4, tmp8)
tmp10 = triton_helpers.maximum(tmp5, tmp9)
tmp13 = tmp12 * tmp2
tmp14 = tl.where(tmp11, tmp4, tmp13)
tmp15 = triton_helpers.maximum(tmp10, tmp14)
tmp18 = tmp17 * tmp2
tmp19 = tl.where(tmp16, tmp4, tmp18)
tmp20 = triton_helpers.maximum(tmp15, tmp19)
tmp21 = tmp5 - tmp20
tmp22 = tl_math.exp(tmp21)
tmp23 = tmp9 - tmp20
tmp24 = tl_math.exp(tmp23)
tmp25 = tmp22 + tmp24
tmp26 = tmp14 - tmp20
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp25 + tmp27
tmp29 = tmp19 - tmp20
tmp30 = tl_math.exp(tmp29)
tmp31 = tmp28 + tmp30
tl.store(out_ptr0 + x0, tmp20, xmask)
tl.store(out_ptr1 + x0, tmp31, xmask)
@triton.jit
def triton_poi_fused__softmax_div_masked_fill_3(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask).to(tl.int1)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp6 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp2 = 1.0
tmp3 = tmp1 * tmp2
tmp4 = -9.999999960041972e-13
tmp5 = tl.where(tmp0, tmp4, tmp3)
tmp7 = tmp5 - tmp6
tmp8 = tl_math.exp(tmp7)
tmp10 = tmp8 / tmp9
tl.store(in_out_ptr0 + x2, tmp10, xmask)
@triton.jit
def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_add_mean_std_5(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 + tmp8
tmp10 = tmp6 + tmp9
tmp13 = tmp11 + tmp12
tmp14 = tmp10 + tmp13
tmp15 = 4.0
tmp16 = tmp14 / tmp15
tmp17 = tmp2 - tmp16
tmp18 = tmp17 * tmp17
tmp19 = tmp5 - tmp16
tmp20 = tmp19 * tmp19
tmp21 = tmp18 + tmp20
tmp22 = tmp9 - tmp16
tmp23 = tmp22 * tmp22
tmp24 = tmp21 + tmp23
tmp25 = tmp13 - tmp16
tmp26 = tmp25 * tmp25
tmp27 = tmp24 + tmp26
tmp28 = 3.0
tmp29 = tmp27 / tmp28
tl.store(in_out_ptr0 + x0, tmp29, xmask)
tl.store(out_ptr0 + x0, tmp16, xmask)
@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr2 + x2, xmask)
tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 - tmp4
tmp7 = libdevice.sqrt(tmp6)
tmp8 = 1e-12
tmp9 = tmp7 + tmp8
tmp10 = tmp5 / tmp9
tmp11 = tmp0 * tmp10
tmp13 = tmp11 + tmp12
tl.store(out_ptr0 + x2, tmp13, xmask)
@triton.jit
def triton_poi_fused_add_7(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_8(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 4.0
tmp10 = tmp8 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = 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-12
tmp27 = tmp25 + tmp26
tmp28 = tmp11 / tmp27
tmp29 = tmp0 * tmp28
tmp31 = tmp29 + tmp30
tl.store(out_ptr0 + x2, tmp31, xmask)
@triton.jit
def triton_poi_fused_relu_threshold_backward_9(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17,
primals_18, primals_19, primals_20, primals_21, primals_22,
primals_23, primals_24, primals_25, primals_26, primals_27,
primals_28, primals_29, primals_30) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4), (4, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_9, (4, 4), (4, 1))
assert_size_stride(primals_10, (4,), (1,))
assert_size_stride(primals_11, (4,), (1,))
assert_size_stride(primals_12, (4,), (1,))
assert_size_stride(primals_13, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_14, (4, 4), (4, 1))
assert_size_stride(primals_15, (4,), (1,))
assert_size_stride(primals_16, (4, 4), (4, 1))
assert_size_stride(primals_17, (4,), (1,))
assert_size_stride(primals_18, (4, 4), (4, 1))
assert_size_stride(primals_19, (4,), (1,))
assert_size_stride(primals_20, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_21, (4, 4), (4, 1))
assert_size_stride(primals_22, (4,), (1,))
assert_size_stride(primals_23, (4,), (1,))
assert_size_stride(primals_24, (4,), (1,))
assert_size_stride(primals_25, (4, 4), (4, 1))
assert_size_stride(primals_26, (4,), (1,))
assert_size_stride(primals_27, (4, 4), (4, 1))
assert_size_stride(primals_28, (4,), (1,))
assert_size_stride(primals_29, (4,), (1,))
assert_size_stride(primals_30, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
del primals_2
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2)
del primals_6
buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(16, 4)](buf0, primals_3, buf3, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_3
buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0)
del buf0
triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5)
buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
triton_poi_fused_eq_1[grid(256)](primals_8, buf6, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_8
buf7 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0)
del buf1
buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf6, buf5,
buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf5
triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf9, buf6,
buf7, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1)
buf10 = reinterpret_tensor(buf8, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf8
triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf10, 16, 4,
XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_7
buf11 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0)
del buf2
extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1),
0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11)
buf12 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf7
triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0)
del buf11
extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4),
(4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0),
alpha=1, beta=1, out=buf13)
del primals_10
buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
buf15 = buf14
del buf14
buf16 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
triton_poi_fused_add_mean_std_5[grid(16)](buf15, buf13, primals_1,
buf16, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_div_mean_mul_std_sub_6[grid(64)](primals_11,
buf13, primals_1, buf16, buf15, primals_12, buf17, 64, XBLOCK=
64, num_warps=1, num_stages=1)
del buf15
del buf16
del primals_12
buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf17, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_14, (4, 4), (1, 4), 0), out=buf18)
buf19 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_13, (16, 4), (4, 1), 0
), reinterpret_tensor(primals_16, (4, 4), (1, 4), 0), out=buf19)
del primals_16
buf20 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_13, (16, 4), (4, 1), 0
), reinterpret_tensor(primals_18, (4, 4), (1, 4), 0), out=buf20)
del primals_18
buf21 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
triton_poi_fused_clone_0[grid(16, 4)](buf18, primals_15, buf21, 16,
4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_15
buf22 = reinterpret_tensor(buf18, (4, 4, 1, 4), (16, 4, 4, 1), 0)
del buf18
triton_poi_fused_clone_0[grid(16, 4)](buf19, primals_17, buf22, 16,
4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_17
buf23 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf21, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf22, (16, 1, 4), (4, 0, 1), 0), out=buf23)
buf24 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
triton_poi_fused_eq_1[grid(256)](primals_20, buf24, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_20
buf25 = reinterpret_tensor(buf19, (4, 4, 4, 1), (16, 4, 1, 64), 0)
del buf19
buf26 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf24, buf23,
buf25, buf26, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf27 = reinterpret_tensor(buf23, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf23
triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf27, buf24,
buf25, buf26, 256, XBLOCK=256, num_warps=4, num_stages=1)
buf28 = reinterpret_tensor(buf26, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf26
triton_poi_fused_clone_0[grid(16, 4)](buf20, primals_19, buf28, 16,
4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1)
del primals_19
buf29 = reinterpret_tensor(buf20, (16, 4, 1), (4, 1, 1), 0)
del buf20
extern_kernels.bmm(reinterpret_tensor(buf27, (16, 4, 4), (16, 4, 1),
0), reinterpret_tensor(buf28, (16, 4, 1), (4, 1, 0), 0), out=buf29)
buf30 = reinterpret_tensor(buf25, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf25
triton_poi_fused_clone_4[grid(16, 4)](buf29, buf30, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf31 = reinterpret_tensor(buf29, (16, 4), (4, 1), 0)
del buf29
extern_kernels.mm(reinterpret_tensor(buf30, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_21, (4, 4), (1, 4), 0), out=buf31)
buf32 = reinterpret_tensor(buf31, (4, 4, 4), (16, 4, 1), 0)
del buf31
triton_poi_fused_add_7[grid(64)](buf32, primals_22, buf17, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_22
buf33 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_div_mean_mul_std_sub_8[grid(64)](primals_23,
buf32, primals_24, buf33, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_24
buf34 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf33, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_25, (4, 4), (1, 4), 0), out=buf34)
buf35 = reinterpret_tensor(buf34, (4, 4, 4), (16, 4, 1), 0)
del buf34
buf39 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_9[grid(64)](buf35,
primals_26, buf39, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_26
buf36 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf35, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_27, (4, 4), (1, 4), 0), out=buf36)
buf37 = reinterpret_tensor(buf36, (4, 4, 4), (16, 4, 1), 0)
del buf36
triton_poi_fused_add_7[grid(64)](buf37, primals_28, buf33, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_28
buf38 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_div_mean_mul_std_sub_8[grid(64)](primals_29,
buf37, primals_30, buf38, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_30
return (buf38, primals_1, primals_11, primals_23, primals_29, buf6,
buf9, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), buf13,
reinterpret_tensor(buf17, (16, 4), (4, 1), 0), reinterpret_tensor(
primals_13, (16, 4), (4, 1), 0), buf24, buf27, reinterpret_tensor(
buf30, (16, 4), (4, 1), 0), buf32, reinterpret_tensor(buf33, (16, 4
), (4, 1), 0), reinterpret_tensor(buf35, (16, 4), (4, 1), 0), buf37,
primals_27, buf39, primals_25, primals_21, reinterpret_tensor(buf28,
(16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf21, (16, 1, 4), (4,
1, 1), 0), reinterpret_tensor(buf22, (16, 4, 1), (4, 1, 4), 0),
primals_14, primals_9, reinterpret_tensor(buf10, (16, 1, 4), (4, 1,
1), 0), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0),
reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0))
class LayerNorm(nn.Module):
def __init__(self, d_model, eps=1e-12):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(d_model))
self.beta = nn.Parameter(torch.zeros(d_model))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
out = (x - mean) / (std + self.eps)
out = self.gamma * out + self.beta
return out
class ScaleDotProductAttention(nn.Module):
"""
compute scale dot product attention
Query : given sentence that we focused on (decoder)
Key : every sentence to check relationship with Qeury(encoder)
Value : every sentence same with Key (encoder)
"""
def __init__(self):
super(ScaleDotProductAttention, self).__init__()
self.softmax = nn.Softmax(dim=-1)
def forward(self, q, k, v, mask=None, e=1e-12):
_batch_size, _head, _length, d_tensor = k.size()
k_t = k.transpose(2, 3)
score = q @ k_t / math.sqrt(d_tensor)
if mask is not None:
score = score.masked_fill(mask == 0, -e)
score = self.softmax(score)
v = score @ v
return v, score
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_head):
super(MultiHeadAttention, self).__init__()
self.n_head = n_head
self.attention = ScaleDotProductAttention()
self.w_q = nn.Linear(d_model, d_model)
self.w_k = nn.Linear(d_model, d_model)
self.w_v = nn.Linear(d_model, d_model)
self.w_concat = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
q, k, v = self.w_q(q), self.w_k(k), self.w_v(v)
q, k, v = self.split(q), self.split(k), self.split(v)
out, _attention = self.attention(q, k, v, mask=mask)
out = self.concat(out)
out = self.w_concat(out)
return out
def split(self, tensor):
"""
split tensor by number of head
:param tensor: [batch_size, length, d_model]
:return: [batch_size, head, length, d_tensor]
"""
batch_size, length, d_model = tensor.size()
d_tensor = d_model // self.n_head
tensor = tensor.view(batch_size, length, self.n_head, d_tensor
).transpose(1, 2)
return tensor
def concat(self, tensor):
"""
inverse function of self.split(tensor : torch.Tensor)
:param tensor: [batch_size, head, length, d_tensor]
:return: [batch_size, length, d_model]
"""
batch_size, head, length, d_tensor = tensor.size()
d_model = head * d_tensor
tensor = tensor.transpose(1, 2).contiguous().view(batch_size,
length, d_model)
return tensor
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, hidden, drop_prob=0.1):
super(PositionwiseFeedForward, self).__init__()
self.linear1 = nn.Linear(d_model, hidden)
self.linear2 = nn.Linear(hidden, d_model)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=drop_prob)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linear2(x)
return x
class DecoderLayerNew(nn.Module):
def __init__(self, d_model, ffn_hidden, n_head, drop_prob):
super(DecoderLayerNew, self).__init__()
self.self_attention = MultiHeadAttention(d_model=d_model, n_head=n_head
)
self.norm1 = LayerNorm(d_model=d_model)
self.dropout1 = nn.Dropout(p=drop_prob)
self.enc_dec_attention = MultiHeadAttention(d_model=d_model, n_head
=n_head)
self.norm2 = LayerNorm(d_model=d_model)
self.dropout2 = nn.Dropout(p=drop_prob)
self.ffn = PositionwiseFeedForward(d_model=d_model, hidden=
ffn_hidden, drop_prob=drop_prob)
self.norm3 = LayerNorm(d_model=d_model)
self.dropout3 = nn.Dropout(p=drop_prob)
def forward(self, input_0, input_1, input_2, input_3):
primals_2 = self.self_attention.w_q.weight
primals_3 = self.self_attention.w_q.bias
primals_4 = self.self_attention.w_k.weight
primals_5 = self.self_attention.w_k.bias
primals_6 = self.self_attention.w_v.weight
primals_7 = self.self_attention.w_v.bias
primals_9 = self.self_attention.w_concat.weight
primals_10 = self.self_attention.w_concat.bias
primals_11 = self.norm1.gamma
primals_12 = self.norm1.beta
primals_14 = self.enc_dec_attention.w_q.weight
primals_15 = self.enc_dec_attention.w_q.bias
primals_16 = self.enc_dec_attention.w_k.weight
primals_17 = self.enc_dec_attention.w_k.bias
primals_18 = self.enc_dec_attention.w_v.weight
primals_19 = self.enc_dec_attention.w_v.bias
primals_21 = self.enc_dec_attention.w_concat.weight
primals_22 = self.enc_dec_attention.w_concat.bias
primals_23 = self.norm2.gamma
primals_24 = self.norm2.beta
primals_25 = self.ffn.linear1.weight
primals_26 = self.ffn.linear1.bias
primals_27 = self.ffn.linear2.weight
primals_28 = self.ffn.linear2.bias
primals_29 = self.norm3.gamma
primals_30 = self.norm3.beta
primals_1 = input_0
primals_13 = input_1
primals_8 = input_2
primals_20 = input_3
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20, primals_21, primals_22, primals_23, primals_24,
primals_25, primals_26, primals_27, primals_28, primals_29,
primals_30])
return output[0]
|
hyunwoongko/transformer
|
DecoderLayer
| false
| 15,609
|
[
"Apache-2.0"
] | 233
|
8f7aaa19d37b088c156db0512868127ba9bf1a0f
|
https://github.com/hyunwoongko/transformer/tree/8f7aaa19d37b088c156db0512868127ba9bf1a0f
|
MLP
|
import torch
import torch.nn as nn
class MLP(nn.Module):
def __init__(self, left_channel, right_channel, out_channel):
super(MLP, self).__init__()
self.left = nn.Linear(left_channel, 128)
self.right = nn.Linear(right_channel, 128)
self.l1 = nn.Linear(256, 256)
self.l2 = nn.Linear(256, out_channel)
def forward(self, left, right):
left_res = self.left(left)
right_res = self.right(right)
tmp = torch.cat([left_res, right_res], dim=1)
tmp = torch.relu(tmp)
tmp = torch.relu(self.l1(tmp))
tmp = self.l2(tmp)
return tmp
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'left_channel': 4, 'right_channel': 4, 'out_channel': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 % 256
x1 = xindex // 256
x2 = xindex
tmp0 = x0
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 128, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (128 * x1 + x0), tmp4 & xmask, eviction_policy
='evict_last', other=0.0)
tmp6 = tl.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], 256, tl.int64)
tmp15 = tl.load(in_ptr2 + (128 * x1 + (-128 + x0)), tmp12 & xmask,
eviction_policy='evict_last', other=0.0)
tmp16 = tl.load(in_ptr3 + (-128 + x0), tmp12 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp17 = tmp15 + tmp16
tmp18 = triton_helpers.maximum(tmp8, tmp17)
tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
tmp20 = tl.where(tmp12, tmp18, tmp19)
tmp21 = tl.where(tmp4, tmp11, tmp20)
tl.store(out_ptr0 + x2, tmp21, xmask)
@triton.jit
def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 256
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_relu_threshold_backward_2(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
x2 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10) = args
args.clear()
assert_size_stride(primals_1, (128, 4), (4, 1))
assert_size_stride(primals_2, (128,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (128, 4), (4, 1))
assert_size_stride(primals_5, (128,), (1,))
assert_size_stride(primals_6, (4, 4), (4, 1))
assert_size_stride(primals_7, (256, 256), (256, 1))
assert_size_stride(primals_8, (256,), (1,))
assert_size_stride(primals_9, (4, 256), (256, 1))
assert_size_stride(primals_10, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 128),
(1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
extern_kernels.mm(primals_6, reinterpret_tensor(primals_4, (4, 128),
(1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((4, 256), (256, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(1024)](buf0, primals_2, buf1, primals_5,
buf2, 1024, XBLOCK=256, num_warps=4, num_stages=1)
buf3 = empty_strided_cuda((4, 256), (256, 1), torch.float32)
extern_kernels.mm(buf2, reinterpret_tensor(primals_7, (256, 256), (
1, 256), 0), out=buf3)
buf4 = buf3
del buf3
triton_poi_fused_relu_1[grid(1024)](buf4, primals_8, 1024, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_8
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_10, buf4, reinterpret_tensor(primals_9,
(256, 4), (1, 256), 0), alpha=1, beta=1, out=buf5)
del primals_10
buf6 = empty_strided_cuda((4, 128), (128, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_2[grid(512)](buf1,
primals_5, buf6, 512, XBLOCK=128, num_warps=4, num_stages=1)
del buf1
del primals_5
buf7 = empty_strided_cuda((4, 128), (128, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_2[grid(512)](buf0,
primals_2, buf7, 512, XBLOCK=128, num_warps=4, num_stages=1)
del buf0
del primals_2
return (buf5, primals_3, primals_6, buf2, buf4, primals_9, primals_7,
buf6, buf7)
class MLPNew(nn.Module):
def __init__(self, left_channel, right_channel, out_channel):
super(MLPNew, self).__init__()
self.left = nn.Linear(left_channel, 128)
self.right = nn.Linear(right_channel, 128)
self.l1 = nn.Linear(256, 256)
self.l2 = nn.Linear(256, out_channel)
def forward(self, input_0, input_1):
primals_1 = self.left.weight
primals_2 = self.left.bias
primals_4 = self.right.weight
primals_5 = self.right.bias
primals_7 = self.l1.weight
primals_8 = self.l1.bias
primals_9 = self.l2.weight
primals_10 = self.l2.bias
primals_3 = input_0
primals_6 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9, primals_10])
return output[0]
|
imxian/FlexTensor
|
MLP
| false
| 15,610
|
[
"MIT"
] | 135
|
311af3362856ea1b0073404fffad42c54585c205
|
https://github.com/imxian/FlexTensor/tree/311af3362856ea1b0073404fffad42c54585c205
|
Invertible1x1Conv
|
import torch
from torch.nn import functional as F
from torch.autograd import Variable
import torch.utils.data
class Invertible1x1Conv(torch.nn.Module):
"""
The layer outputs both the convolution, and the log determinant
of its weight matrix. If reverse=True it does convolution with
inverse
"""
def __init__(self, c):
super(Invertible1x1Conv, self).__init__()
self.conv = torch.nn.Conv1d(c, c, kernel_size=1, stride=1, padding=
0, bias=False)
W = torch.qr(torch.FloatTensor(c, c).normal_())[0]
if torch.det(W) < 0:
W[:, 0] = -1 * W[:, 0]
W = W.view(c, c, 1)
W = W.contiguous()
self.conv.weight.data = W
def forward(self, z):
batch_size, _group_size, n_of_groups = z.size()
W = self.conv.weight.squeeze()
log_det_W = batch_size * n_of_groups * torch.logdet(W.unsqueeze(0).
float()).squeeze()
z = self.conv(z)
return z, log_det_W
def infer(self, z):
_batch_size, _group_size, _n_of_groups = z.size()
W = self.conv.weight.squeeze()
if not hasattr(self, 'W_inverse'):
W_inverse = W.float().inverse()
W_inverse = Variable(W_inverse[..., None])
if z.type() == 'torch.cuda.HalfTensor' or z.type(
) == 'torch.HalfTensor':
W_inverse = W_inverse.half()
self.W_inverse = W_inverse
z = F.conv1d(z, self.W_inverse, bias=None, stride=1, padding=0)
return z
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'c': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch.nn import functional as F
from torch.autograd import Variable
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_eq_mul_0(in_out_ptr0, 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])
tmp4 = tl.load(in_out_ptr0 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp2 = -1.0
tmp3 = tmp1 == tmp2
tmp6 = float('nan')
tmp7 = tl.where(tmp3, tmp6, tmp5)
tmp8 = 16.0
tmp9 = tmp7 * tmp8
tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp3, None)
tl.store(in_out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp9, None)
def call(args):
primals_1, primals_2 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = torch.ops.aten._linalg_slogdet.default(reinterpret_tensor(
primals_2, (1, 4, 4), (16, 4, 1), 0))
buf1 = buf0[0]
buf2 = buf0[1]
buf3 = buf0[2]
buf4 = buf0[3]
del buf0
buf5 = empty_strided_cuda((1,), (1,), torch.bool)
buf7 = reinterpret_tensor(buf2, (), (), 0)
del buf2
get_raw_stream(0)
triton_poi_fused_eq_mul_0[grid(1)](buf7, buf1, buf5, 1, XBLOCK=1,
num_warps=1, num_stages=1)
del buf1
buf6 = extern_kernels.convolution(primals_1, primals_2, stride=(1,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf6, (4, 4, 4), (16, 4, 1))
return buf6, buf7, primals_1, primals_2, buf3, buf4, buf5
class Invertible1x1ConvNew(torch.nn.Module):
"""
The layer outputs both the convolution, and the log determinant
of its weight matrix. If reverse=True it does convolution with
inverse
"""
def __init__(self, c):
super(Invertible1x1ConvNew, self).__init__()
self.conv = torch.nn.Conv1d(c, c, kernel_size=1, stride=1, padding=
0, bias=False)
W = torch.qr(torch.FloatTensor(c, c).normal_())[0]
if torch.det(W) < 0:
W[:, 0] = -1 * W[:, 0]
W = W.view(c, c, 1)
W = W.contiguous()
self.conv.weight.data = W
def infer(self, z):
_batch_size, _group_size, _n_of_groups = z.size()
W = self.conv.weight.squeeze()
if not hasattr(self, 'W_inverse'):
W_inverse = W.float().inverse()
W_inverse = Variable(W_inverse[..., None])
if z.type() == 'torch.cuda.HalfTensor' or z.type(
) == 'torch.HalfTensor':
W_inverse = W_inverse.half()
self.W_inverse = W_inverse
z = F.conv1d(z, self.W_inverse, bias=None, stride=1, padding=0)
return z
def forward(self, input_0):
primals_2 = self.conv.weight
primals_1 = input_0
output = call([primals_1, primals_2])
return output[0], output[1]
|
ishalyminov/shad_speech
|
Invertible1x1Conv
| false
| 15,611
|
[
"MIT"
] | 83
|
e1345d2de929e150b2683190b127a837fbcb34f3
|
https://github.com/ishalyminov/shad_speech/tree/e1345d2de929e150b2683190b127a837fbcb34f3
|
Loss
|
import torch
import torch.nn as nn
import torch.utils.data
class Loss(nn.Module):
def __init__(self):
super(Loss, self).__init__()
def forward(self, gt_region, gt_affinity, pred_region, pred_affinity,
conf_map):
loss = torch.mean(((gt_region - pred_region).pow(2) + (gt_affinity -
pred_affinity).pow(2)) * conf_map)
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), 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
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_mean_mul_pow_sub_0(in_out_ptr0, in_ptr0, in_ptr1,
in_ptr2, in_ptr3, in_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)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp4 = tl.load(in_ptr2 + r0, None)
tmp5 = tl.load(in_ptr3 + r0, None)
tmp9 = tl.load(in_ptr4 + r0, None)
tmp2 = tmp0 - tmp1
tmp3 = tmp2 * tmp2
tmp6 = tmp4 - tmp5
tmp7 = tmp6 * tmp6
tmp8 = tmp3 + tmp7
tmp10 = tmp8 * tmp9
tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
tmp14 = 256.0
tmp15 = tmp13 / tmp14
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp15, None)
def call(args):
arg0_1, arg1_1, arg2_1, arg3_1, arg4_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg4_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_mean_mul_pow_sub_0[grid(1)](buf1, arg0_1,
arg1_1, arg2_1, arg3_1, arg4_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
del arg2_1
del arg3_1
del arg4_1
return buf1,
class LossNew(nn.Module):
def __init__(self):
super(LossNew, self).__init__()
def forward(self, input_0, input_1, input_2, input_3, input_4):
arg0_1 = input_0
arg1_1 = input_1
arg2_1 = input_2
arg3_1 = input_3
arg4_1 = input_4
output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1])
return output[0]
|
ishine/EasyOCR
|
Loss
| false
| 15,612
|
[
"Apache-2.0"
] | 56
|
ab7cebb64482e5e50ee7a37fa50398b8cb7481c7
|
https://github.com/ishine/EasyOCR/tree/ab7cebb64482e5e50ee7a37fa50398b8cb7481c7
|
BlockWidth1d
|
import torch
import torch.utils.data
import torch.nn.functional as F
import torch.nn as nn
class BlockWidth1d(nn.Module):
def __init__(self, width) ->None:
super().__init__()
self.conv = nn.Conv1d(width, width, kernel_size=5, padding=2)
def forward(self, x):
x = x + F.leaky_relu(self.conv(x))
return x
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {'width': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.utils.data
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_leaky_relu_0(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
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr2 + x2, xmask)
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp6 = 0.01
tmp7 = tmp2 * tmp6
tmp8 = tl.where(tmp4, tmp2, tmp7)
tmp9 = tmp5 + tmp8
tl.store(out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr1 + x2, tmp9, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 5), (20, 5, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1,
4, 4), (16, 4, 1), 0), primals_1, stride=(1,), padding=(2,),
dilation=(1,), transposed=False, output_padding=(0,), groups=1,
bias=None)
assert_size_stride(buf0, (1, 4, 4), (16, 4, 1))
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_leaky_relu_0[grid(16)](buf0, primals_2,
primals_3, buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1)
del buf0
del primals_2
return buf2, primals_1, reinterpret_tensor(primals_3, (1, 4, 4), (16, 4,
1), 0), buf1
class BlockWidth1dNew(nn.Module):
def __init__(self, width) ->None:
super().__init__()
self.conv = nn.Conv1d(width, width, kernel_size=5, padding=2)
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]
|
ishine/HiFiplusplus-pytorch
|
BlockWidth1d
| false
| 15,613
|
[
"MIT"
] | 69
|
8be0d0e0092d4f609c37bfbeede5a9ad9bd7470a
|
https://github.com/ishine/HiFiplusplus-pytorch/tree/8be0d0e0092d4f609c37bfbeede5a9ad9bd7470a
|
PARALossSoftmax
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class PARALossSoftmax(nn.Module):
"""
Softmax classifier for sentence-level relation extraction.
"""
def __init__(self):
"""
Args:
sentence_encoder: encoder for sentences
num_class: number of classes
id2rel: dictionary of id -> relation name mapping
"""
super().__init__()
def forward(self, score, predicate_one_hot_labels):
soft = True
if predicate_one_hot_labels.is_sparse:
predicate_one_hot_labels = predicate_one_hot_labels.to_dense()
if not soft:
entity_mask = predicate_one_hot_labels.sum(dim=1)
label = predicate_one_hot_labels.argmax(dim=1)
loss = F.cross_entropy(score, label, reduction='none')
loss = loss * entity_mask
loss = loss.sum(dim=(1, 2)) / entity_mask.sum(dim=(1, 2))
loss = loss.mean()
else:
entity_mask = predicate_one_hot_labels.sum(dim=1, keepdim=True
).repeat_interleave(score.shape[1], dim=1).float()
score = (score * entity_mask).sum(dim=(2, 3))
label = predicate_one_hot_labels.sum(dim=(2, 3)).argmax(dim=-1)
loss = F.cross_entropy(score, label)
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
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_sum_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_per_fused_mul_sum_1(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
x3 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
tmp1 = tl.load(in_ptr1 + (r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp2 = tl.load(in_ptr1 + (16 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp4 = tl.load(in_ptr1 + (32 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp6 = tl.load(in_ptr1 + (48 + r2 + 64 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 * tmp7
tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
tmp11 = tl.where(xmask, tmp9, 0)
tmp12 = tl.sum(tmp11, 1)[:, None]
tl.store(out_ptr0 + x3, tmp12, xmask)
@triton.jit
def triton_poi_fused__log_softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_per_fused_argmax_nll_loss_forward_3(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_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
tmp17 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
tmp32 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
tmp56 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
tmp58 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
tmp61 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
tmp64 = tl.load(in_ptr1 + (3 + 4 * r0), None, 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, 1], 0, tl.int64)
tmp11 = tl.full([1, 1], 1, tl.int64)
tmp12 = tmp10 < tmp11
tmp13 = tmp9 & tmp12
tmp14 = tmp7 | tmp13
tmp15 = tl.where(tmp14, tmp0, tmp1)
tmp16 = tl.where(tmp14, tmp10, tmp11)
tmp18 = tmp15 > tmp17
tmp19 = tmp15 == tmp17
tmp20 = tmp15 != tmp15
tmp21 = tmp17 != tmp17
tmp22 = tmp20 > tmp21
tmp23 = tmp18 | tmp22
tmp24 = tmp20 & tmp21
tmp25 = tmp19 | tmp24
tmp26 = tl.full([1, 1], 2, tl.int64)
tmp27 = tmp16 < tmp26
tmp28 = tmp25 & tmp27
tmp29 = tmp23 | tmp28
tmp30 = tl.where(tmp29, tmp15, tmp17)
tmp31 = tl.where(tmp29, tmp16, tmp26)
tmp33 = tmp30 > tmp32
tmp34 = tmp30 == tmp32
tmp35 = tmp30 != tmp30
tmp36 = tmp32 != tmp32
tmp37 = tmp35 > tmp36
tmp38 = tmp33 | tmp37
tmp39 = tmp35 & tmp36
tmp40 = tmp34 | tmp39
tmp41 = tl.full([1, 1], 3, tl.int64)
tmp42 = tmp31 < tmp41
tmp43 = tmp40 & tmp42
tmp44 = tmp38 | tmp43
tl.where(tmp44, tmp30, tmp32)
tmp46 = tl.where(tmp44, tmp31, tmp41)
tmp47 = tl.full([1, 1], -100, tl.int64)
tmp48 = tmp46 != tmp47
tmp49 = tl.where(tmp48, tmp46, tmp10)
tmp50 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
tmp51 = tmp49 + tmp50
tmp52 = tmp49 < 0
tmp53 = tl.where(tmp52, tmp51, tmp49)
tl.device_assert((0 <= tmp53) & (tmp53 < 4),
'index out of bounds: 0 <= tmp53 < 4')
tmp55 = tl.load(in_ptr1 + (tmp53 + 4 * r0), None, eviction_policy=
'evict_last')
tmp57 = tl_math.exp(tmp56)
tmp59 = tl_math.exp(tmp58)
tmp60 = tmp57 + tmp59
tmp62 = tl_math.exp(tmp61)
tmp63 = tmp60 + tmp62
tmp65 = tl_math.exp(tmp64)
tmp66 = tmp63 + tmp65
tmp67 = tl_math.log(tmp66)
tmp68 = tmp55 - tmp67
tmp69 = -tmp68
tmp70 = 0.0
tmp71 = tl.where(tmp48, tmp69, tmp70)
tmp72 = tl.broadcast_to(tmp71, [XBLOCK, RBLOCK])
tmp74 = tl.sum(tmp72, 1)[:, None]
tmp75 = tmp48.to(tl.int64)
tmp76 = tl.broadcast_to(tmp75, [XBLOCK, RBLOCK])
tmp78 = tl.sum(tmp76, 1)[:, None]
tmp79 = tmp78.to(tl.float32)
tmp80 = tmp74 / tmp79
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp80, 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_sum_0[grid(16)](arg0_1, buf0, 16, 16, XBLOCK=8,
num_warps=2, num_stages=1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_per_fused_mul_sum_1[grid(16)](arg1_1, arg0_1, buf2, 16, 16,
XBLOCK=8, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused__log_softmax_2[grid(16)](buf2, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf2
buf4 = empty_strided_cuda((), (), torch.float32)
buf6 = buf4
del buf4
triton_per_fused_argmax_nll_loss_forward_3[grid(1)](buf6, buf0,
buf3, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
del buf0
del buf3
return buf6,
class PARALossSoftmaxNew(nn.Module):
"""
Softmax classifier for sentence-level relation extraction.
"""
def __init__(self):
"""
Args:
sentence_encoder: encoder for sentences
num_class: number of classes
id2rel: dictionary of id -> relation name mapping
"""
super().__init__()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
igorvlnascimento/redn
|
PARALossSoftmax
| false
| 15,614
|
[
"MIT"
] | 100
|
f40f19a0fdfbb11a7987996d520716a05bafd77b
|
https://github.com/igorvlnascimento/redn/tree/f40f19a0fdfbb11a7987996d520716a05bafd77b
|
AttDot
|
import torch
import torch.nn.functional as F
class AttDot(torch.nn.Module):
"""
AttDot: Dot attention that can be used by the Alignment module.
"""
def __init__(self, softmax=True):
super().__init__()
self.softmax = softmax
def forward(self, query, y):
att = torch.bmm(query, y.transpose(2, 1))
sim = att.max(2)[0].unsqueeze(1)
if self.softmax:
att = F.softmax(att, dim=2)
return att, sim
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_max_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
tmp2 = triton_helpers.maximum(tmp0, tmp1)
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp6 = triton_helpers.maximum(tmp4, tmp5)
tl.store(out_ptr0 + x0, tmp6, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(arg1_1, reinterpret_tensor(arg0_1, (4, 4, 4), (
16, 1, 4), 0), out=buf0)
del arg0_1
del arg1_1
buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__softmax_0[grid(64)](buf0, buf1, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(64)](buf1, buf2, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf1
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_max_2[grid(16)](buf0, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf0
return buf2, reinterpret_tensor(buf3, (4, 1, 4), (4, 4, 1), 0)
class AttDotNew(torch.nn.Module):
"""
AttDot: Dot attention that can be used by the Alignment module.
"""
def __init__(self, softmax=True):
super().__init__()
self.softmax = softmax
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0], output[1]
|
ishine/NISQA
|
AttDot
| false
| 15,615
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
AttentionPool
|
import torch
import torch.nn as nn
class AttentionPool(nn.Module):
"""docstring for AttentionPool"""
def __init__(self, inputdim, outputdim=10, pooldim=1, **kwargs):
super().__init__()
self.inputdim = inputdim
self.outputdim = outputdim
self.pooldim = pooldim
self.transform = nn.Linear(inputdim, outputdim)
self.activ = nn.Softmax(dim=self.pooldim)
self.eps = 1e-07
def forward(self, logits, decision):
w = self.activ(torch.clamp(self.transform(logits), -15, 15))
detect = (decision * w).sum(self.pooldim) / (w.sum(self.pooldim) +
self.eps)
return detect
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 10])]
def get_init_inputs():
return [[], {'inputdim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused__softmax_clamp_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
x3 = xindex
x0 = xindex % 40
x2 = xindex // 160
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp5 = tl.load(in_ptr0 + (x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr0 + (40 + x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp12 = tl.load(in_ptr0 + (80 + x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp16 = tl.load(in_ptr0 + (120 + x0 + 160 * x2), xmask, eviction_policy
='evict_last')
tmp1 = -15.0
tmp2 = triton_helpers.maximum(tmp0, tmp1)
tmp3 = 15.0
tmp4 = triton_helpers.minimum(tmp2, tmp3)
tmp6 = triton_helpers.maximum(tmp5, tmp1)
tmp7 = triton_helpers.minimum(tmp6, tmp3)
tmp9 = triton_helpers.maximum(tmp8, tmp1)
tmp10 = triton_helpers.minimum(tmp9, tmp3)
tmp11 = triton_helpers.maximum(tmp7, tmp10)
tmp13 = triton_helpers.maximum(tmp12, tmp1)
tmp14 = triton_helpers.minimum(tmp13, tmp3)
tmp15 = triton_helpers.maximum(tmp11, tmp14)
tmp17 = triton_helpers.maximum(tmp16, tmp1)
tmp18 = triton_helpers.minimum(tmp17, tmp3)
tmp19 = triton_helpers.maximum(tmp15, tmp18)
tmp20 = tmp4 - tmp19
tmp21 = tl_math.exp(tmp20)
tl.store(out_ptr0 + x3, tmp21, xmask)
@triton.jit
def triton_poi_fused__softmax_1(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
x3 = xindex
x0 = xindex % 40
x2 = xindex // 160
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (40 + x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (80 + x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (120 + x0 + 160 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_div_mul_sum_2(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 160
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 40
x1 = xindex // 40
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 160 * x1), xmask)
tmp1 = tl.load(in_ptr1 + (x0 + 160 * x1), xmask)
tmp3 = tl.load(in_ptr0 + (40 + x0 + 160 * x1), xmask)
tmp4 = tl.load(in_ptr1 + (40 + x0 + 160 * x1), xmask)
tmp7 = tl.load(in_ptr0 + (80 + x0 + 160 * x1), xmask)
tmp8 = tl.load(in_ptr1 + (80 + x0 + 160 * x1), xmask)
tmp11 = tl.load(in_ptr0 + (120 + x0 + 160 * x1), xmask)
tmp12 = tl.load(in_ptr1 + (120 + x0 + 160 * x1), xmask)
tmp2 = tmp0 * tmp1
tmp5 = tmp3 * tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 * tmp8
tmp10 = tmp6 + tmp9
tmp13 = tmp11 * tmp12
tmp14 = tmp10 + tmp13
tmp15 = tmp1 + tmp4
tmp16 = tmp15 + tmp8
tmp17 = tmp16 + tmp12
tmp18 = 1e-07
tmp19 = tmp17 + tmp18
tmp20 = tmp14 / tmp19
tl.store(out_ptr0 + x2, tmp20, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (10, 4), (4, 1))
assert_size_stride(primals_2, (10,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4, 4, 10), (160, 40, 10, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 10), (10, 1), torch.float32)
extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64,
4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 10), (1, 4),
0), alpha=1, beta=1, out=buf0)
del primals_1
del primals_2
buf1 = empty_strided_cuda((4, 4, 4, 10), (160, 40, 10, 1), torch.
float32)
get_raw_stream(0)
triton_poi_fused__softmax_clamp_0[grid(640)](buf0, buf1, 640,
XBLOCK=256, num_warps=4, num_stages=1)
buf2 = empty_strided_cuda((4, 4, 4, 10), (160, 40, 10, 1), torch.
float32)
triton_poi_fused__softmax_1[grid(640)](buf1, buf2, 640, XBLOCK=256,
num_warps=4, num_stages=1)
del buf1
buf3 = empty_strided_cuda((4, 4, 10), (40, 10, 1), torch.float32)
triton_poi_fused_add_div_mul_sum_2[grid(160)](primals_4, buf2, buf3,
160, XBLOCK=256, num_warps=4, num_stages=1)
del buf2
return buf3, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), buf0
class AttentionPoolNew(nn.Module):
"""docstring for AttentionPool"""
def __init__(self, inputdim, outputdim=10, pooldim=1, **kwargs):
super().__init__()
self.inputdim = inputdim
self.outputdim = outputdim
self.pooldim = pooldim
self.transform = nn.Linear(inputdim, outputdim)
self.activ = nn.Softmax(dim=self.pooldim)
self.eps = 1e-07
def forward(self, input_0, input_1):
primals_1 = self.transform.weight
primals_2 = self.transform.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
ishine/AudioCaption
|
AttentionPool
| false
| 15,616
|
[
"MIT"
] | 76
|
d121cba8247b96aeed9ff77d2fff073f93e0a63f
|
https://github.com/ishine/AudioCaption/tree/d121cba8247b96aeed9ff77d2fff073f93e0a63f
|
Conv1DBlock
|
import torch
import torch.nn.functional as F
import torch.nn as nn
class ConvNorm(nn.Module):
""" 1D Convolution """
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
padding=None, dilation=1, bias=True, w_init_gain='linear'):
super(ConvNorm, self).__init__()
if padding is None:
assert kernel_size % 2 == 1
padding = int(dilation * (kernel_size - 1) / 2)
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=
kernel_size, stride=stride, padding=padding, dilation=dilation,
bias=bias)
def forward(self, signal):
conv_signal = self.conv(signal)
return conv_signal
class Conv1DBlock(nn.Module):
""" 1D Convolutional Block """
def __init__(self, in_channels, out_channels, kernel_size, activation=
None, dropout=None):
super(Conv1DBlock, self).__init__()
self.conv_layer = nn.Sequential()
self.conv_layer.add_module('conv_layer', ConvNorm(in_channels,
out_channels, kernel_size=kernel_size, stride=1, padding=int((
kernel_size - 1) / 2), dilation=1, w_init_gain='tanh'))
if activation is not None:
self.conv_layer.add_module('activ', activation)
self.dropout = dropout
def forward(self, x, mask=None):
x = x.contiguous().transpose(1, 2)
x = self.conv_layer(x)
if self.dropout is not None:
x = F.dropout(x, self.dropout, self.training)
x = x.contiguous().transpose(1, 2)
if mask is not None:
x = x.masked_fill(mask.unsqueeze(-1), 0)
return x
def get_inputs():
return [torch.rand([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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_convolution_0(in_ptr0, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 3 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_convolution_0[grid(16, 4)](primals_1, buf0, 16, 4,
XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,),
padding=(1,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 3), (12, 3, 1))
del buf0
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(48)](buf2, primals_3, 48,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return reinterpret_tensor(buf2, (4, 3, 4), (12, 1, 3), 0
), primals_2, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0)
class ConvNorm(nn.Module):
""" 1D Convolution """
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
padding=None, dilation=1, bias=True, w_init_gain='linear'):
super(ConvNorm, self).__init__()
if padding is None:
assert kernel_size % 2 == 1
padding = int(dilation * (kernel_size - 1) / 2)
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=
kernel_size, stride=stride, padding=padding, dilation=dilation,
bias=bias)
def forward(self, signal):
conv_signal = self.conv(signal)
return conv_signal
class Conv1DBlockNew(nn.Module):
""" 1D Convolutional Block """
def __init__(self, in_channels, out_channels, kernel_size, activation=
None, dropout=None):
super(Conv1DBlockNew, self).__init__()
self.conv_layer = nn.Sequential()
self.conv_layer.add_module('conv_layer', ConvNorm(in_channels,
out_channels, kernel_size=kernel_size, stride=1, padding=int((
kernel_size - 1) / 2), dilation=1, w_init_gain='tanh'))
if activation is not None:
self.conv_layer.add_module('activ', activation)
self.dropout = dropout
def forward(self, input_0):
primals_1 = self.conv_layer.conv_layer.conv.weight
primals_3 = self.conv_layer.conv_layer.conv.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/FastPitchFormant
|
Conv1DBlock
| false
| 15,617
|
[
"MIT"
] | 54
|
dd86032953be04fb526b658b19ecdc5600ff25a5
|
https://github.com/ishine/FastPitchFormant/tree/dd86032953be04fb526b658b19ecdc5600ff25a5
|
TokenLearnedEncoding
|
import torch
from torch import nn
class TokenLearnedEncoding(nn.Module):
"""
Learned additive img/word/action token encoding implemented on top of nn.Embedding
"""
def __init__(self, d_model, vocab_size=3, init_range=0.1):
super().__init__()
self.emb = nn.Embedding(vocab_size, d_model)
self.emb.weight.data.uniform_(-init_range, init_range)
def forward(self, lang, frames, actions):
token_lang = torch.ones(lang.shape[:2], device=lang.device, dtype=
torch.long) * 0
token_lang_emb = self.emb(token_lang)
lang += token_lang_emb
token_frames = torch.ones(frames.shape[:2], device=frames.device,
dtype=torch.long) * 1
token_frames_emb = self.emb(token_frames)
frames += token_frames_emb
token_actions = torch.ones(actions.shape[:2], device=actions.device,
dtype=torch.long) * 2
token_actions_emb = self.emb(token_actions)
actions += token_actions_emb
return lang, frames, actions
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 [[], {'d_model': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_mul_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 = tl.full([1], 0, tl.int64)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_embedding_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
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)
@triton.jit
def triton_poi_fused_mul_2(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.full([1], 1, tl.int64)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_embedding_3(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(out_ptr0 + x2, tmp2, xmask)
@triton.jit
def triton_poi_fused_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 = tl.full([1], 2, tl.int64)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_embedding_5(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + (8 + 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, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (3, 4), (4, 1))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 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, 4), (4, 1), torch.int64)
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](buf0, 16, XBLOCK=16, num_warps=1,
num_stages=1)
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_embedding_1[grid(256)](primals_1, primals_2,
buf1, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
triton_poi_fused_mul_2[grid(16)](buf2, 16, XBLOCK=16, num_warps=1,
num_stages=1)
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_embedding_3[grid(256)](primals_3, primals_2,
buf3, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_3
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
triton_poi_fused_mul_4[grid(16)](buf4, 16, XBLOCK=16, num_warps=1,
num_stages=1)
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_embedding_5[grid(256)](primals_4, primals_2,
buf5, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
del primals_4
return buf1, buf3, buf5, buf1, buf3, buf5, buf0, buf2, buf4
class TokenLearnedEncodingNew(nn.Module):
"""
Learned additive img/word/action token encoding implemented on top of nn.Embedding
"""
def __init__(self, d_model, vocab_size=3, init_range=0.1):
super().__init__()
self.emb = nn.Embedding(vocab_size, d_model)
self.emb.weight.data.uniform_(-init_range, init_range)
def forward(self, input_0, input_1, input_2):
primals_2 = self.emb.weight
primals_1 = input_0
primals_3 = input_1
primals_4 = input_2
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0], output[1], output[2]
|
ishikasingh/teach
|
TokenLearnedEncoding
| false
| 15,618
|
[
"MIT"
] | 54
|
5554f02f55c22abfe5c2a749dbb24c13377726c8
|
https://github.com/ishikasingh/teach/tree/5554f02f55c22abfe5c2a749dbb24c13377726c8
|
BlockWidth2d
|
import torch
import torch.utils.data
import torch.nn.functional as F
import torch.nn as nn
class BlockWidth2d(nn.Module):
def __init__(self, width) ->None:
super().__init__()
self.conv = nn.Conv2d(width, width, kernel_size=3, padding=1)
def forward(self, x):
x = x + F.leaky_relu(self.conv(x))
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'width': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.utils.data
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_convolution_leaky_relu_0(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr2 + x3, xmask)
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp6 = 0.01
tmp7 = tmp2 * tmp6
tmp8 = tl.where(tmp4, tmp2, tmp7)
tmp9 = tmp5 + tmp8
tl.store(out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr1 + x3, tmp9, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1,
1), padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_convolution_leaky_relu_0[grid(256)](buf0,
primals_2, primals_3, buf1, buf2, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del buf0
del primals_2
return buf2, primals_1, primals_3, buf1
class BlockWidth2dNew(nn.Module):
def __init__(self, width) ->None:
super().__init__()
self.conv = nn.Conv2d(width, width, kernel_size=3, padding=1)
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]
|
ishine/HiFiplusplus-pytorch
|
BlockWidth2d
| false
| 15,619
|
[
"MIT"
] | 69
|
8be0d0e0092d4f609c37bfbeede5a9ad9bd7470a
|
https://github.com/ishine/HiFiplusplus-pytorch/tree/8be0d0e0092d4f609c37bfbeede5a9ad9bd7470a
|
ApplyHardAttention
|
import torch
class ApplyHardAttention(torch.nn.Module):
"""
ApplyHardAttention: Apply hard attention for the purpose of time-alignment.
"""
def __init__(self):
super().__init__()
def forward(self, y, att):
self.idx = att.argmax(2)
y = y[torch.arange(y.shape[0]).unsqueeze(-1), self.idx]
return y
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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 % 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)
tmp17 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
tmp32 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
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 + x2, tmp46, xmask)
@triton.jit
def triton_poi_fused_index_1(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
x4 = xindex // 16
x0 = xindex % 16
x2 = xindex // 64 % 4
x5 = xindex
tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last')
tmp1 = tl.full([XBLOCK], 4, tl.int32)
tmp2 = tmp0 + tmp1
tmp3 = tmp0 < 0
tmp4 = tl.where(tmp3, tmp2, tmp0)
tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
'index out of bounds: 0 <= tmp4 < 4')
tmp6 = tl.load(in_ptr1 + (x0 + 16 * tmp4 + 64 * x2), xmask)
tl.store(out_ptr0 + x5, tmp6, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 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
buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_index_1[grid(1024)](buf0, arg1_1, buf1, 1024,
XBLOCK=256, num_warps=4, num_stages=1)
del arg1_1
return buf1, buf0
class ApplyHardAttentionNew(torch.nn.Module):
"""
ApplyHardAttention: Apply hard attention for the purpose of time-alignment.
"""
def __init__(self):
super().__init__()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ishine/NISQA
|
ApplyHardAttention
| false
| 15,620
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
EmissionModel
|
import torch
from torch import nn
import torch.distributions as tdist
class EmissionModel(nn.Module):
"""
Emission Model of the HMM, it represents the probability of emitting an observation based on the current state
"""
def __init__(self):
super(EmissionModel, self).__init__()
self.distribution_function = tdist.normal.Normal
def sample(self, means, stds):
"""
Draws a Sample from each distribution
"""
return self.distribution_function(means, stds).sample()
def forward(self, x_t, means, stds, state_lengths):
"""
Calculates the log probability of the the given data (x_t) being observed from states
Args:
x_t (float tensor) : observation at current time step
shape: (batch, feature_dim)
means (float tensor): means of the distributions of hidden states
shape: (batch, hidden_state, feature_dim)
stds (float tensor): standard deviations of the distributions of the hidden states
shape: (feature_dim) tdist.normal.Normal will broadcast to the shape needed
state_lengths (int tensor): Lengths of states in a batch
shape: (batch)
Returns:
out (float tensor): observation log likelihoods, expressing the probability of an observation
being generated from a state i
shape: (batch, hidden_state)
"""
T_max = means.shape[1]
emission_dists = self.distribution_function(means, stds)
x_t = x_t.unsqueeze(1)
out = emission_dists.log_prob(x_t)
mask_tensor = x_t.new_zeros(T_max)
state_lengths_mask = (torch.arange(T_max, out=mask_tensor).expand(
len(state_lengths), T_max) < state_lengths.unsqueeze(1)).unsqueeze(
2)
out = torch.sum(out * state_lengths_mask, dim=2)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import nn
import torch.distributions as tdist
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_div_log_mul_neg_pow_sub_sum_0(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x4 = xindex % 256
x6 = xindex % 64
x3 = xindex // 256
x0 = xindex % 4
x7 = xindex
tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr2 + x6, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr3 + (x6 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp20 = tl.load(in_ptr1 + (64 + x6), xmask, eviction_policy='evict_last')
tmp24 = tl.load(in_ptr2 + (64 + x6), xmask, eviction_policy='evict_last')
tmp33 = tl.load(in_ptr1 + (128 + x6), xmask, eviction_policy='evict_last')
tmp37 = tl.load(in_ptr2 + (128 + x6), xmask, eviction_policy='evict_last')
tmp46 = tl.load(in_ptr1 + (192 + x6), xmask, eviction_policy='evict_last')
tmp50 = tl.load(in_ptr2 + (192 + x6), xmask, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp3 = tmp2 * tmp2
tmp4 = -tmp3
tmp6 = tmp5 * tmp5
tmp7 = 2.0
tmp8 = tmp6 * tmp7
tmp9 = tmp4 / tmp8
tmp10 = tl_math.log(tmp5)
tmp11 = tmp9 - tmp10
tmp12 = 0.9189385332046727
tmp13 = tmp11 - tmp12
tmp15 = x0
tmp16 = tmp15.to(tl.float32)
tmp17 = tmp16 < tmp14
tmp18 = tmp17.to(tl.float32)
tmp19 = tmp13 * tmp18
tmp21 = tmp0 - tmp20
tmp22 = tmp21 * tmp21
tmp23 = -tmp22
tmp25 = tmp24 * tmp24
tmp26 = tmp25 * tmp7
tmp27 = tmp23 / tmp26
tmp28 = tl_math.log(tmp24)
tmp29 = tmp27 - tmp28
tmp30 = tmp29 - tmp12
tmp31 = tmp30 * tmp18
tmp32 = tmp19 + tmp31
tmp34 = tmp0 - tmp33
tmp35 = tmp34 * tmp34
tmp36 = -tmp35
tmp38 = tmp37 * tmp37
tmp39 = tmp38 * tmp7
tmp40 = tmp36 / tmp39
tmp41 = tl_math.log(tmp37)
tmp42 = tmp40 - tmp41
tmp43 = tmp42 - tmp12
tmp44 = tmp43 * tmp18
tmp45 = tmp32 + tmp44
tmp47 = tmp0 - tmp46
tmp48 = tmp47 * tmp47
tmp49 = -tmp48
tmp51 = tmp50 * tmp50
tmp52 = tmp51 * tmp7
tmp53 = tmp49 / tmp52
tmp54 = tl_math.log(tmp50)
tmp55 = tmp53 - tmp54
tmp56 = tmp55 - tmp12
tmp57 = tmp56 * tmp18
tmp58 = tmp45 + tmp57
tl.store(out_ptr0 + x7, tmp58, xmask)
def call(args):
arg0_1, arg1_1, arg2_1, arg3_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_div_log_mul_neg_pow_sub_sum_0[grid(1024)](arg2_1,
arg0_1, arg1_1, arg3_1, buf0, 1024, XBLOCK=128, num_warps=4,
num_stages=1)
del arg0_1
del arg1_1
del arg2_1
del arg3_1
return buf0,
class EmissionModelNew(nn.Module):
"""
Emission Model of the HMM, it represents the probability of emitting an observation based on the current state
"""
def __init__(self):
super(EmissionModelNew, self).__init__()
self.distribution_function = tdist.normal.Normal
def sample(self, means, stds):
"""
Draws a Sample from each distribution
"""
return self.distribution_function(means, stds).sample()
def forward(self, input_0, input_1, input_2, input_3):
arg0_1 = input_0
arg1_1 = input_1
arg2_1 = input_2
arg3_1 = input_3
output = call([arg0_1, arg1_1, arg2_1, arg3_1])
return output[0]
|
ishine/Neural-HMM
|
EmissionModel
| false
| 15,621
|
[
"MIT"
] | 66
|
c0bc23ab88f831173d2d4db29a84503b80c5cdc4
|
https://github.com/ishine/Neural-HMM/tree/c0bc23ab88f831173d2d4db29a84503b80c5cdc4
|
StyleEmbedAttention
|
import torch
import torch.nn.functional as F
import torch.nn as nn
class StyleEmbedAttention(nn.Module):
""" StyleEmbedAttention """
def __init__(self, query_dim, key_dim, num_units, num_heads):
super(StyleEmbedAttention, self).__init__()
self.num_units = num_units
self.num_heads = num_heads
self.key_dim = key_dim
self.W_query = nn.Linear(in_features=query_dim, out_features=
num_units, bias=False)
self.W_key = nn.Linear(in_features=key_dim, out_features=num_units,
bias=False)
self.W_value = nn.Linear(in_features=key_dim, out_features=
num_units, bias=False)
def forward(self, query, key_soft):
"""
input:
query --- [N, T_q, query_dim]
key_soft --- [N, T_k, key_dim]
output:
out --- [N, T_q, num_units]
"""
values = self.W_value(key_soft)
split_size = self.num_units // self.num_heads
values = torch.stack(torch.split(values, split_size, dim=2), dim=0)
out_soft = scores_soft = None
querys = self.W_query(query)
keys = self.W_key(key_soft)
querys = torch.stack(torch.split(querys, split_size, dim=2), dim=0)
keys = torch.stack(torch.split(keys, split_size, dim=2), dim=0)
scores_soft = torch.matmul(querys, keys.transpose(2, 3))
scores_soft = scores_soft / self.key_dim ** 0.5
scores_soft = F.softmax(scores_soft, dim=3)
out_soft = torch.matmul(scores_soft, values)
out_soft = torch.cat(torch.split(out_soft, 1, dim=0), dim=3).squeeze(0)
return out_soft
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'query_dim': 4, 'key_dim': 4, 'num_units': 4, 'num_heads': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex // 4
x0 = xindex % 4
x1 = xindex // 4 % 4
x2 = xindex // 16
x4 = xindex
tmp0 = x3
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 + 4 * x2)), 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 + 4 * x2)), 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 + 4 * x2)), 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 + 4 * x2)),
tmp16 & xmask, eviction_policy='evict_last', other=0.0)
tmp20 = tl.where(tmp14, tmp15, tmp19)
tmp21 = tl.where(tmp9, tmp10, tmp20)
tmp22 = tl.where(tmp4, tmp5, tmp21)
tmp23 = 0.7071067811865476
tmp24 = tmp22 * tmp23
tl.store(out_ptr0 + x4, tmp24, xmask)
@triton.jit
def triton_poi_fused_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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_stack_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
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_cat_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
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 = 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))
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_2, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf2)
del primals_5
buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_0[grid(64)](buf1, buf3, 64, XBLOCK=64, num_warps=1,
num_stages=1)
buf4 = reinterpret_tensor(buf1, (4, 4, 1, 4), (16, 4, 4, 1), 0)
del buf1
triton_poi_fused_0[grid(64)](buf2, buf4, 64, XBLOCK=64, num_warps=1,
num_stages=1)
buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5)
buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_1[grid(256)](buf5, buf6, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_2[grid(256)](buf5, buf6, buf7, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf5
del buf6
buf8 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0)
del buf2
triton_poi_fused_stack_3[grid(64)](buf0, buf8, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf9 = reinterpret_tensor(buf0, (16, 4, 1), (4, 1, 1), 0)
del buf0
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((1, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_cat_4[grid(64)](buf9, buf10, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf9
return reinterpret_tensor(buf10, (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
), 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 StyleEmbedAttentionNew(nn.Module):
""" StyleEmbedAttention """
def __init__(self, query_dim, key_dim, num_units, num_heads):
super(StyleEmbedAttentionNew, self).__init__()
self.num_units = num_units
self.num_heads = num_heads
self.key_dim = key_dim
self.W_query = nn.Linear(in_features=query_dim, out_features=
num_units, bias=False)
self.W_key = nn.Linear(in_features=key_dim, out_features=num_units,
bias=False)
self.W_value = nn.Linear(in_features=key_dim, out_features=
num_units, bias=False)
def forward(self, input_0, input_1):
primals_1 = self.W_query.weight
primals_3 = self.W_key.weight
primals_5 = self.W_value.weight
primals_2 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ishine/Comprehensive-Transformer-TTS
|
StyleEmbedAttention
| false
| 15,622
|
[
"MIT"
] | 147
|
dca252cae50a18464ce2410aa85a21c557c72d7a
|
https://github.com/ishine/Comprehensive-Transformer-TTS/tree/dca252cae50a18464ce2410aa85a21c557c72d7a
|
FCMinibatchStd
|
import math
import torch
from torch import nn
from torch.nn import functional as F
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class FCMinibatchStd(nn.Module):
def __init__(self, in_channel, out_channel):
super().__init__()
self.fc = EqualLinear(in_channel + 1, out_channel, activation=
'fused_lrelu')
def forward(self, out):
stddev = torch.sqrt(out.var(0, unbiased=False) + 1e-08).mean().view(
1, 1).repeat(out.size(0), 1)
out = torch.cat([out, stddev], 1)
out = self.fc(out)
return out
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4, 'out_channel': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import math
from torch import nn
from torch.nn import functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_add_mean_repeat_sqrt_var_0(in_ptr0, out_ptr1, xnumel,
rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 4
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr0 + (4 + r0), None)
tmp3 = tl.load(in_ptr0 + (8 + r0), None)
tmp5 = tl.load(in_ptr0 + (12 + r0), None)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp7 = 4.0
tmp8 = tmp6 / tmp7
tmp9 = tmp0 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tmp1 - tmp8
tmp12 = tmp11 * tmp11
tmp13 = tmp10 + tmp12
tmp14 = tmp3 - tmp8
tmp15 = tmp14 * tmp14
tmp16 = tmp13 + tmp15
tmp17 = tmp5 - tmp8
tmp18 = tmp17 * tmp17
tmp19 = tmp16 + tmp18
tmp20 = tmp19 / tmp7
tmp21 = 1e-08
tmp22 = tmp20 + tmp21
tmp23 = libdevice.sqrt(tmp22)
tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
tmp26 = tl.sum(tmp24, 1)[:, None]
tmp27 = tmp26 / tmp7
tl.store(out_ptr1 + tl.broadcast_to(5 * r0, [XBLOCK, RBLOCK]), tmp27, None)
@triton.jit
def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tl.store(out_ptr0 + (x0 + 5 * x1), tmp0, xmask)
@triton.jit
def triton_poi_fused_mul_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 20
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.4472135954999579
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_add_leaky_relu_mul_3(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp2 = 1.0
tmp3 = tmp1 * tmp2
tmp4 = tmp0 + tmp3
tmp5 = 0.0
tmp6 = tmp4 > tmp5
tmp7 = 0.2
tmp8 = tmp4 * tmp7
tmp9 = tl.where(tmp6, tmp4, tmp8)
tmp10 = 1.4142135623730951
tmp11 = tmp9 * tmp10
tl.store(out_ptr0 + x2, tmp6, xmask)
tl.store(out_ptr1 + x2, tmp11, 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, 5), (5, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf3 = empty_strided_cuda((4, 5), (5, 1), torch.float32)
buf2 = reinterpret_tensor(buf3, (4, 1), (5, 1), 4)
get_raw_stream(0)
triton_per_fused_add_mean_repeat_sqrt_var_0[grid(1)](primals_1,
buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
buf1 = reinterpret_tensor(buf3, (4, 4), (5, 1), 0)
triton_poi_fused_cat_1[grid(16)](primals_1, buf1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_1
buf4 = empty_strided_cuda((4, 5), (5, 1), torch.float32)
triton_poi_fused_mul_2[grid(20)](primals_3, buf4, 20, XBLOCK=32,
num_warps=1, num_stages=1)
del primals_3
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(buf3, reinterpret_tensor(buf4, (5, 4), (1, 5), 0),
out=buf5)
del buf4
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_leaky_relu_mul_3[grid(16)](buf5, primals_2,
buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1)
del buf5
del primals_2
return buf7, buf3, buf6
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class FCMinibatchStdNew(nn.Module):
def __init__(self, in_channel, out_channel):
super().__init__()
self.fc = EqualLinear(in_channel + 1, out_channel, activation=
'fused_lrelu')
def forward(self, input_0):
primals_3 = self.fc.weight
primals_2 = self.fc.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/GANsNRoses
|
FCMinibatchStd
| false
| 15,623
|
[
"MIT"
] | 969
|
414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
https://github.com/ishine/GANsNRoses/tree/414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
ModulatedConv2d
|
import math
import torch
from torch import nn
from torch.nn import functional as F
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4, 'out_channel': 4, 'kernel_size': 4,
'style_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import math
from torch import nn
from torch.nn import functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_mul_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_per_fused_add_mul_pow_rsqrt_sum_2(in_out_ptr0, in_ptr0, in_ptr1,
out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r5 = rindex
x0 = xindex % 4
r3 = rindex // 16
x1 = xindex // 4
x4 = xindex
tmp0 = tl.load(in_ptr0 + (r5 + 64 * x0), xmask, eviction_policy=
'evict_last', other=0.0)
tmp3 = tl.load(in_ptr1 + (r3 + 4 * x1), xmask, eviction_policy=
'evict_last', other=0.0)
tmp1 = 0.125
tmp2 = tmp0 * tmp1
tmp4 = tmp2 * tmp3
tmp5 = tmp4 * tmp4
tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
tmp8 = tl.where(xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = 1e-08
tmp11 = tmp9 + tmp10
tmp12 = libdevice.rsqrt(tmp11)
tmp13 = tmp4 * tmp12
tl.debug_barrier()
tl.store(in_out_ptr0 + x4, tmp12, xmask)
tl.store(out_ptr0 + (r5 + 64 * x4), tmp13, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](primals_3, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_3
buf1 = empty_strided_cuda((4,), (1,), torch.float32)
triton_poi_fused_mul_1[grid(4)](primals_2, buf1, 4, XBLOCK=4,
num_warps=1, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(buf1, primals_4, reinterpret_tensor(buf0, (4,
4), (1, 4), 0), alpha=1, beta=1, out=buf2)
del buf1
buf3 = buf0
del buf0
buf4 = buf3
del buf3
buf5 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_per_fused_add_mul_pow_rsqrt_sum_2[grid(16)](buf4, primals_5,
buf2, buf5, 16, 64, XBLOCK=8, num_warps=4, num_stages=1)
buf6 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf5, (16, 4,
4, 4), (64, 16, 4, 1), 0), stride=(1, 1), padding=(2, 2),
dilation=(1, 1), transposed=False, output_padding=(0, 0),
groups=4, bias=None)
assert_size_stride(buf6, (1, 16, 5, 5), (400, 25, 5, 1))
return reinterpret_tensor(buf6, (4, 4, 5, 5), (100, 25, 5, 1), 0
), primals_4, primals_5, buf2, buf4, reinterpret_tensor(buf5, (16,
4, 4, 4), (64, 16, 4, 1), 0), reinterpret_tensor(primals_1, (1, 16,
4, 4), (256, 16, 4, 1), 0)
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2dNew(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input_0, input_1):
primals_5 = self.weight
primals_3 = self.modulation.weight
primals_2 = self.modulation.bias
primals_1 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ishine/GANsNRoses
|
ModulatedConv2d
| false
| 15,624
|
[
"MIT"
] | 969
|
414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
https://github.com/ishine/GANsNRoses/tree/414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
StyleAdaptiveLayerNorm
|
import torch
import torch.nn as nn
import torch.utils.data.distributed
class AffineLinear(nn.Module):
def __init__(self, in_dim, out_dim):
super(AffineLinear, self).__init__()
affine = nn.Linear(in_dim, out_dim)
self.affine = affine
def forward(self, input):
return self.affine(input)
class StyleAdaptiveLayerNorm(nn.Module):
def __init__(self, in_channel, style_dim):
super(StyleAdaptiveLayerNorm, self).__init__()
self.in_channel = in_channel
self.norm = nn.LayerNorm(in_channel, elementwise_affine=False)
self.style = AffineLinear(style_dim, in_channel * 2)
self.style.affine.bias.data[:in_channel] = 1
self.style.affine.bias.data[in_channel:] = 0
def forward(self, input, style_code):
style = self.style(style_code).unsqueeze(1)
gamma, beta = style.chunk(2, dim=-1)
out = self.norm(input)
out = gamma * out + beta
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4, 'style_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
import torch.utils.data.distributed
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_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_add_mul_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4 % 16
x3 = xindex // 256
x4 = xindex % 256
x5 = xindex // 4 % 64
x7 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1 + 128 * x3), xmask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr4 + x5, xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr0 + (4 + x0 + 8 * x1 + 128 * x3), xmask,
eviction_policy='evict_last')
tmp10 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp5 = tmp3 - tmp4
tmp7 = tmp5 * tmp6
tmp8 = tmp2 * tmp7
tmp11 = tmp9 + tmp10
tmp12 = tmp8 + tmp11
tl.store(out_ptr0 + x7, tmp12, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (8, 4), (4, 1))
assert_size_stride(primals_2, (8,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 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((64, 8), (8, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0)
del primals_1
buf1 = 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)](primals_4, buf1,
buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_add_mul_native_layer_norm_1[grid(1024)](buf0,
primals_2, primals_4, buf1, buf2, buf3, 1024, XBLOCK=256,
num_warps=4, num_stages=1)
del buf0
del buf1
del buf2
del primals_2
return buf3, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0)
class AffineLinear(nn.Module):
def __init__(self, in_dim, out_dim):
super(AffineLinear, self).__init__()
affine = nn.Linear(in_dim, out_dim)
self.affine = affine
def forward(self, input):
return self.affine(input)
class StyleAdaptiveLayerNormNew(nn.Module):
def __init__(self, in_channel, style_dim):
super(StyleAdaptiveLayerNormNew, self).__init__()
self.in_channel = in_channel
self.norm = nn.LayerNorm(in_channel, elementwise_affine=False)
self.style = AffineLinear(style_dim, in_channel * 2)
self.style.affine.bias.data[:in_channel] = 1
self.style.affine.bias.data[in_channel:] = 0
def forward(self, input_0, input_1):
primals_1 = self.style.affine.weight
primals_2 = self.style.affine.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
ishine/StyleSpeech-1
|
StyleAdaptiveLayerNorm
| false
| 15,625
|
[
"MIT"
] | 106
|
f939cf9cb981db7b738fa9c9c9a7fea2dfdd0766
|
https://github.com/ishine/StyleSpeech-1/tree/f939cf9cb981db7b738fa9c9c9a7fea2dfdd0766
|
_DynamicGates
|
from _paritybench_helpers import _mock_config
import torch
import torch.nn as nn
class _DynamicGates(nn.Module):
"""Internal class to wrap the dynamic gate parameters into a dedicated PyTorch Module"""
def __init__(self, cfg: 'Config', input_size: 'int'):
super(_DynamicGates, self).__init__()
self.cfg = cfg
self.weight_ih = nn.Parameter(torch.FloatTensor(input_size, 3 * cfg
.hidden_size))
self.weight_hh = nn.Parameter(torch.FloatTensor(cfg.hidden_size, 3 *
cfg.hidden_size))
self.bias = nn.Parameter(torch.FloatTensor(3 * cfg.hidden_size))
self._reset_parameters()
def _reset_parameters(self):
"""Special initialization of certain model weights."""
nn.init.orthogonal_(self.weight_ih.data)
weight_hh_data = torch.eye(self.cfg.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 3)
self.weight_hh.data = weight_hh_data
nn.init.constant_(self.bias.data, val=0)
if self.cfg.initial_forget_bias is not None:
self.bias.data[:self.cfg.hidden_size
] = self.cfg.initial_forget_bias
def forward(self, h: 'torch.Tensor', x_d: 'torch.Tensor'):
gates = h @ self.weight_hh + x_d @ self.weight_ih + self.bias
return gates
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'cfg': _mock_config(hidden_size=4, initial_forget_bias=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
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 768
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 12
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x2, xmask)
tmp3 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 12), (12, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4, 12), (12, 1))
assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_5, (12,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 12), (12, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0),
primals_1, out=buf0)
del primals_1
buf1 = empty_strided_cuda((64, 12), (12, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (4, 1), 0),
primals_3, out=buf1)
del primals_3
buf2 = reinterpret_tensor(buf0, (4, 4, 4, 12), (192, 48, 12, 1), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_add_0[grid(768)](buf2, buf1, primals_5, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del buf1
del primals_5
return buf2, reinterpret_tensor(primals_4, (4, 64), (1, 4), 0
), reinterpret_tensor(primals_2, (4, 64), (1, 4), 0)
class _DynamicGatesNew(nn.Module):
"""Internal class to wrap the dynamic gate parameters into a dedicated PyTorch Module"""
def __init__(self, cfg: 'Config', input_size: 'int'):
super(_DynamicGatesNew, self).__init__()
self.cfg = cfg
self.weight_ih = nn.Parameter(torch.FloatTensor(input_size, 3 * cfg
.hidden_size))
self.weight_hh = nn.Parameter(torch.FloatTensor(cfg.hidden_size, 3 *
cfg.hidden_size))
self.bias = nn.Parameter(torch.FloatTensor(3 * cfg.hidden_size))
self._reset_parameters()
def _reset_parameters(self):
"""Special initialization of certain model weights."""
nn.init.orthogonal_(self.weight_ih.data)
weight_hh_data = torch.eye(self.cfg.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 3)
self.weight_hh.data = weight_hh_data
nn.init.constant_(self.bias.data, val=0)
if self.cfg.initial_forget_bias is not None:
self.bias.data[:self.cfg.hidden_size
] = self.cfg.initial_forget_bias
def forward(self, input_0, input_1):
primals_1 = self.weight_ih
primals_3 = self.weight_hh
primals_5 = self.bias
primals_2 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DavidChoi76/neuralhydrology
|
_DynamicGates
| false
| 15,626
|
[
"BSD-3-Clause"
] | 144
|
a4c284b92934ee973c8b3fedf8a60df60c8feae1
|
https://github.com/DavidChoi76/neuralhydrology/tree/a4c284b92934ee973c8b3fedf8a60df60c8feae1
|
FastAttention
|
import torch
import torch.nn as nn
class FastAttention(nn.Module):
""" wuch15's Fastformer Attention module (Official) """
def __init__(self, dim, dim_head, heads, dropout=0.1, initializer_range
=0.02):
super(FastAttention, self).__init__()
self.initializer_range = initializer_range
if dim % dim_head != 0:
raise ValueError(
'The hidden size (%d) is not a multiple of the number of attention heads (%d)'
% (dim, dim_head))
self.attention_head_size = int(dim / dim_head)
self.num_attention_heads = dim_head
self.all_head_size = (self.num_attention_heads * self.
attention_head_size)
self.input_dim = dim
self.query = nn.Linear(self.input_dim, self.all_head_size)
self.to_q_attn_logits = nn.Linear(self.all_head_size, self.
num_attention_heads)
self.key = nn.Linear(self.input_dim, self.all_head_size)
self.to_k_attn_logits = nn.Linear(self.all_head_size, self.
num_attention_heads)
self.transform = nn.Linear(self.all_head_size, self.all_head_size)
self.softmax = nn.Softmax(dim=-1)
self.apply(self.init_weights)
self.dropout = nn.Dropout(dropout)
def init_weights(self, module):
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=self.initializer_range)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
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, mask):
"""
hidden_states -- [B, T, H]
mask -- [B, T]
"""
mask = mask.unsqueeze(1)
mask = mask
mask = (1.0 - mask) * -10000.0
_batch_size, seq_len, _ = hidden_states.shape
mixed_query_layer = self.query(hidden_states)
mixed_key_layer = self.key(hidden_states)
query_for_score = self.to_q_attn_logits(mixed_query_layer).transpose(
1, 2) / self.attention_head_size ** 0.5
query_for_score += mask
query_weight = self.softmax(query_for_score).unsqueeze(2)
query_layer = self.transpose_for_scores(mixed_query_layer)
pooled_query = torch.matmul(query_weight, query_layer).transpose(1, 2
).view(-1, 1, self.num_attention_heads * self.attention_head_size)
pooled_query_repeat = pooled_query.repeat(1, seq_len, 1)
mixed_query_key_layer = mixed_key_layer * pooled_query_repeat
query_key_score = (self.to_k_attn_logits(mixed_query_key_layer) /
self.attention_head_size ** 0.5).transpose(1, 2)
query_key_score += mask
query_key_weight = self.softmax(query_key_score).unsqueeze(2)
key_layer = self.transpose_for_scores(mixed_query_key_layer)
pooled_key = torch.matmul(query_key_weight, key_layer)
weighted_value = (pooled_key * query_layer).transpose(1, 2)
weighted_value = weighted_value.reshape(weighted_value.size()[:-2] +
(self.num_attention_heads * self.attention_head_size,))
weighted_value = self.transform(weighted_value) + mixed_query_layer
return self.dropout(weighted_value)
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'dim': 4, 'dim_head': 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__softmax_add_div_mul_rsub_0(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 % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr2 + 4 * x1, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
tmp13 = tl.load(in_ptr2 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
)
tmp18 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
tmp21 = tl.load(in_ptr2 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
)
tmp26 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
tmp29 = tl.load(in_ptr2 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp3 = 1.0
tmp4 = tmp2 * tmp3
tmp6 = tmp3 - tmp5
tmp7 = -10000.0
tmp8 = tmp6 * tmp7
tmp9 = tmp4 + tmp8
tmp11 = tmp10 + tmp1
tmp12 = tmp11 * tmp3
tmp14 = tmp3 - tmp13
tmp15 = tmp14 * tmp7
tmp16 = tmp12 + tmp15
tmp17 = triton_helpers.maximum(tmp9, tmp16)
tmp19 = tmp18 + tmp1
tmp20 = tmp19 * tmp3
tmp22 = tmp3 - tmp21
tmp23 = tmp22 * tmp7
tmp24 = tmp20 + tmp23
tmp25 = triton_helpers.maximum(tmp17, tmp24)
tmp27 = tmp26 + tmp1
tmp28 = tmp27 * tmp3
tmp30 = tmp3 - tmp29
tmp31 = tmp30 * tmp7
tmp32 = tmp28 + tmp31
tmp33 = triton_helpers.maximum(tmp25, tmp32)
tmp34 = tmp9 - tmp33
tmp35 = tl_math.exp(tmp34)
tmp36 = tmp16 - tmp33
tmp37 = tl_math.exp(tmp36)
tmp38 = tmp35 + tmp37
tmp39 = tmp24 - tmp33
tmp40 = tl_math.exp(tmp39)
tmp41 = tmp38 + tmp40
tmp42 = tmp32 - tmp33
tmp43 = tl_math.exp(tmp42)
tmp44 = tmp41 + tmp43
tl.store(out_ptr0 + x2, tmp33, xmask)
tl.store(out_ptr1 + x2, tmp44, xmask)
@triton.jit
def triton_poi_fused__softmax_add_div_mul_rsub_1(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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')
tmp5 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr3 + y3, ymask, eviction_policy='evict_last')
tmp13 = tl.load(in_ptr4 + y3, ymask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 1.0
tmp4 = tmp2 * tmp3
tmp6 = tmp3 - tmp5
tmp7 = -10000.0
tmp8 = tmp6 * tmp7
tmp9 = tmp4 + tmp8
tmp11 = tmp9 - tmp10
tmp12 = tl_math.exp(tmp11)
tmp14 = tmp12 / tmp13
tl.store(out_ptr0 + (x2 + 4 * y3), tmp14, xmask & ymask)
@triton.jit
def triton_poi_fused_clone_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_mul_repeat_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 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp1 = tl.load(in_ptr1 + x3, xmask)
tmp2 = tmp1 * tmp0
tl.store(out_ptr0 + x3, tmp0, xmask)
tl.store(out_ptr1 + x3, tmp2, xmask)
@triton.jit
def triton_poi_fused_mul_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex // 16
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp1 = tl.load(in_ptr1 + x3, xmask)
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x3, tmp2, xmask)
@triton.jit
def triton_poi_fused_add_5(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12
) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 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, 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,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (16,
4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0
), alpha=1, beta=1, out=buf0)
del primals_3
del primals_4
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_6, reinterpret_tensor(primals_2, (16,
4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0
), alpha=1, beta=1, out=buf1)
del primals_5
del primals_6
buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(buf0, reinterpret_tensor(primals_7, (4, 4), (1, 4
), 0), out=buf2)
buf3 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused__softmax_add_div_mul_rsub_0[grid(16)](buf2,
primals_8, primals_1, buf3, buf4, 16, XBLOCK=16, num_warps=1,
num_stages=1)
buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_add_div_mul_rsub_1[grid(16, 4)](buf2,
primals_8, primals_1, buf3, buf4, buf5, 16, 4, XBLOCK=4, YBLOCK
=16, num_warps=1, num_stages=1)
del primals_8
buf6 = reinterpret_tensor(buf2, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf2
triton_poi_fused_clone_2[grid(16, 4)](buf0, buf6, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf7 = reinterpret_tensor(buf4, (16, 1, 1), (1, 1, 1), 0)
del buf4
extern_kernels.bmm(reinterpret_tensor(buf5, (16, 1, 4), (4, 4, 1),
0), reinterpret_tensor(buf6, (16, 4, 1), (4, 1, 0), 0), out=buf7)
buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_mul_repeat_3[grid(64)](buf7, buf1, buf8, buf9, 64,
XBLOCK=64, num_warps=1, num_stages=1)
buf10 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf9, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf10)
buf11 = reinterpret_tensor(buf7, (4, 4, 1), (4, 1, 16), 0)
del buf7
buf12 = buf3
del buf3
triton_poi_fused__softmax_add_div_mul_rsub_0[grid(16)](buf10,
primals_10, primals_1, buf11, buf12, 16, XBLOCK=16, num_warps=1,
num_stages=1)
buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_add_div_mul_rsub_1[grid(16, 4)](buf10,
primals_10, primals_1, buf11, buf12, buf13, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
del buf11
del primals_1
del primals_10
buf14 = reinterpret_tensor(buf10, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf10
triton_poi_fused_clone_2[grid(16, 4)](buf9, buf14, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf15 = reinterpret_tensor(buf12, (16, 1, 1), (1, 1, 1), 0)
del buf12
extern_kernels.bmm(reinterpret_tensor(buf13, (16, 1, 4), (4, 4, 1),
0), reinterpret_tensor(buf14, (16, 4, 1), (4, 1, 0), 0), out=buf15)
buf16 = empty_strided_cuda((4, 4, 4, 1), (16, 1, 4, 16), torch.float32)
triton_poi_fused_mul_4[grid(64)](buf15, buf0, buf16, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf17 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf16, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_11, (4, 4), (1, 4), 0), out=buf17)
buf18 = reinterpret_tensor(buf17, (4, 4, 4), (16, 4, 1), 0)
del buf17
triton_poi_fused_add_5[grid(64)](buf18, primals_12, buf0, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_12
return buf18, reinterpret_tensor(primals_2, (16, 4), (4, 1), 0
), buf0, buf1, buf5, buf8, reinterpret_tensor(buf9, (16, 4), (4, 1), 0
), buf13, buf15, reinterpret_tensor(buf16, (16, 4), (4, 1), 0
), primals_11, reinterpret_tensor(buf14, (16, 1, 4), (4, 1, 1), 0
), primals_9, reinterpret_tensor(buf6, (16, 1, 4), (4, 1, 1), 0
), primals_7
class FastAttentionNew(nn.Module):
""" wuch15's Fastformer Attention module (Official) """
def __init__(self, dim, dim_head, heads, dropout=0.1, initializer_range
=0.02):
super(FastAttentionNew, self).__init__()
self.initializer_range = initializer_range
if dim % dim_head != 0:
raise ValueError(
'The hidden size (%d) is not a multiple of the number of attention heads (%d)'
% (dim, dim_head))
self.attention_head_size = int(dim / dim_head)
self.num_attention_heads = dim_head
self.all_head_size = (self.num_attention_heads * self.
attention_head_size)
self.input_dim = dim
self.query = nn.Linear(self.input_dim, self.all_head_size)
self.to_q_attn_logits = nn.Linear(self.all_head_size, self.
num_attention_heads)
self.key = nn.Linear(self.input_dim, self.all_head_size)
self.to_k_attn_logits = nn.Linear(self.all_head_size, self.
num_attention_heads)
self.transform = nn.Linear(self.all_head_size, self.all_head_size)
self.softmax = nn.Softmax(dim=-1)
self.apply(self.init_weights)
self.dropout = nn.Dropout(dropout)
def init_weights(self, module):
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=self.initializer_range)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
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_4 = self.query.bias
primals_3 = self.to_q_attn_logits.weight
primals_6 = self.to_q_attn_logits.bias
primals_5 = self.key.weight
primals_8 = self.key.bias
primals_7 = self.to_k_attn_logits.weight
primals_10 = self.to_k_attn_logits.bias
primals_9 = self.transform.weight
primals_12 = self.transform.bias
primals_2 = input_0
primals_11 = 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]
|
ishine/Comprehensive-Transformer-TTS
|
FastAttention
| false
| 15,627
|
[
"MIT"
] | 147
|
dca252cae50a18464ce2410aa85a21c557c72d7a
|
https://github.com/ishine/Comprehensive-Transformer-TTS/tree/dca252cae50a18464ce2410aa85a21c557c72d7a
|
GeGLU
|
from _paritybench_helpers import _mock_config
import torch
import torch.nn as nn
from torch.nn import functional as F
class GeGLU(torch.nn.Module):
def __init__(self, config, layer_id, time_shift=False):
super().__init__()
self.layer_id = layer_id
if time_shift:
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
hidden_sz = 3 * config.n_ffn
self.key = nn.Linear(config.n_embd, hidden_sz)
self.value = nn.Linear(config.n_embd, hidden_sz)
self.weight = nn.Linear(hidden_sz, config.n_embd)
def forward(self, x):
_B, _T, C = x.size()
if hasattr(self, 'time_shift'):
x = torch.cat([self.time_shift(x[:, :, :C // 2]), x[:, :, C //
2:]], dim=-1)
k = self.key(x)
v = self.value(x)
y = self.weight(F.gelu(k) * v)
return y
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'config': _mock_config(n_ffn=4, n_embd=4), 'layer_id': 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
assert_size_stride = torch._C._dynamo.guards.assert_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_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 192
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp9 = tl.load(in_ptr1 + 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
tmp10 = tmp8 * tmp9
tl.store(out_ptr0 + x0, tmp10, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (12, 4), (4, 1))
assert_size_stride(primals_3, (12,), (1,))
assert_size_stride(primals_4, (12, 4), (4, 1))
assert_size_stride(primals_5, (12,), (1,))
assert_size_stride(primals_6, (4, 12), (12, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 12), (12, 1), torch.float32)
extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (16,
4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 12), (1, 4),
0), alpha=1, beta=1, out=buf0)
del primals_2
del primals_3
buf1 = empty_strided_cuda((16, 12), (12, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(primals_1, (16,
4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 12), (1, 4),
0), alpha=1, beta=1, out=buf1)
del primals_4
del primals_5
buf2 = empty_strided_cuda((4, 4, 12), (48, 12, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_gelu_mul_0[grid(192)](buf0, buf1, buf2, 192,
XBLOCK=256, num_warps=4, num_stages=1)
buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf2, (16, 12),
(12, 1), 0), reinterpret_tensor(primals_6, (12, 4), (1, 12), 0),
alpha=1, beta=1, out=buf3)
del primals_7
return reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0
), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0
), buf0, buf1, reinterpret_tensor(buf2, (16, 12), (12, 1), 0
), primals_6
class GeGLUNew(torch.nn.Module):
def __init__(self, config, layer_id, time_shift=False):
super().__init__()
self.layer_id = layer_id
if time_shift:
self.time_shift = nn.ZeroPad2d((0, 0, 1, -1))
hidden_sz = 3 * config.n_ffn
self.key = nn.Linear(config.n_embd, hidden_sz)
self.value = nn.Linear(config.n_embd, hidden_sz)
self.weight = nn.Linear(hidden_sz, config.n_embd)
def forward(self, input_0):
primals_2 = self.key.weight
primals_3 = self.key.bias
primals_4 = self.value.weight
primals_5 = self.value.bias
primals_6 = self.weight.weight
primals_7 = self.weight.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
BlinkDL/RWKV-LM
|
GeGLU
| false
| 15,628
|
[
"BSD-2-Clause"
] | 102
|
b48aa1d430a71ced8ae6a665c47f5dbd95f6f6ab
|
https://github.com/BlinkDL/RWKV-LM/tree/b48aa1d430a71ced8ae6a665c47f5dbd95f6f6ab
|
StyledResBlock
|
import math
import torch
from torch import nn
from torch.nn import functional as F
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class FusedLeakyReLU(nn.Module):
def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
super().__init__()
self.bias = nn.Parameter(torch.zeros(channel))
self.negative_slope = negative_slope
self.scale = scale
def forward(self, input):
return fused_leaky_relu(input, self.bias, self.negative_slope, self
.scale)
class StyledConv(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, upsample=False, downsample=False, blur_kernel=[1, 3,
3, 1], demodulate=True):
super().__init__()
self.use_style = use_style
self.conv = ModulatedConv2d(in_channel, out_channel, kernel_size,
style_dim, use_style=use_style, upsample=upsample, downsample=
downsample, blur_kernel=blur_kernel, demodulate=demodulate)
self.activate = FusedLeakyReLU(out_channel)
def forward(self, input, style=None, noise=None):
out = self.conv(input, style)
out = self.activate(out)
return out
class StyledResBlock(nn.Module):
def __init__(self, in_channel, style_dim, blur_kernel=[1, 3, 3, 1],
demodulate=True):
super().__init__()
self.conv1 = StyledConv(in_channel, in_channel, 3, style_dim,
upsample=False, blur_kernel=blur_kernel, demodulate=demodulate)
self.conv2 = StyledConv(in_channel, in_channel, 3, style_dim,
upsample=False, blur_kernel=blur_kernel, demodulate=demodulate)
def forward(self, input, style):
out = self.conv1(input, style)
out = self.conv2(out, style)
out = (out + input) / math.sqrt(2)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4, 'style_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import math
from torch import nn
from torch.nn import functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_mul_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_per_fused_add_mul_pow_rsqrt_sum_2(in_out_ptr0, in_ptr0, in_ptr1,
out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
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
r5 = rindex
x0 = xindex % 4
r3 = rindex // 9
x1 = xindex // 4
x4 = xindex
tmp0 = tl.load(in_ptr0 + (r5 + 36 * x0), rmask & xmask, eviction_policy
='evict_last', other=0.0)
tmp3 = tl.load(in_ptr1 + (r3 + 4 * x1), rmask & xmask, eviction_policy=
'evict_last', other=0.0)
tmp1 = 0.16666666666666666
tmp2 = tmp0 * tmp1
tmp4 = tmp2 * tmp3
tmp5 = tmp4 * tmp4
tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
tmp8 = tl.where(rmask & xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = 1e-08
tmp11 = tmp9 + tmp10
tmp12 = libdevice.rsqrt(tmp11)
tmp13 = tmp4 * tmp12
tl.debug_barrier()
tl.store(in_out_ptr0 + x4, tmp12, xmask)
tl.store(out_ptr0 + (r5 + 36 * x4), tmp13, rmask & xmask)
@triton.jit
def triton_poi_fused_add_leaky_relu_mul_3(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.2
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tmp8 = 1.4142135623730951
tmp9 = tmp7 * tmp8
tl.store(out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr1 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused_add_div_leaky_relu_mul_4(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr2 + x3, xmask)
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.2
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tmp8 = 1.4142135623730951
tmp9 = tmp7 * tmp8
tmp11 = tmp9 + tmp10
tmp12 = 0.7071067811865475
tmp13 = tmp11 * tmp12
tl.store(out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr1 + x3, tmp13, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
assert_size_stride(primals_6, (4,), (1,))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4), (4, 1))
assert_size_stride(primals_9, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
assert_size_stride(primals_10, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](primals_3, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_3
buf1 = empty_strided_cuda((4,), (1,), torch.float32)
triton_poi_fused_mul_1[grid(4)](primals_2, buf1, 4, XBLOCK=4,
num_warps=1, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(buf1, primals_4, reinterpret_tensor(buf0, (4,
4), (1, 4), 0), alpha=1, beta=1, out=buf2)
buf3 = buf0
del buf0
buf4 = buf3
del buf3
buf5 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
torch.float32)
triton_per_fused_add_mul_pow_rsqrt_sum_2[grid(16)](buf4, primals_5,
buf2, buf5, 16, 36, XBLOCK=1, num_warps=2, num_stages=1)
buf6 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf5, (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(buf6, (1, 16, 4, 4), (256, 16, 4, 1))
buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
buf14 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_leaky_relu_mul_3[grid(256)](buf6, primals_6,
buf7, buf14, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_6
buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_mul_0[grid(16)](primals_8, buf8, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_8
buf9 = buf1
del buf1
triton_poi_fused_mul_1[grid(4)](primals_7, buf9, 4, XBLOCK=4,
num_warps=1, num_stages=1)
del primals_7
buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(buf9, primals_4, reinterpret_tensor(buf8, (4,
4), (1, 4), 0), alpha=1, beta=1, out=buf10)
del buf9
buf11 = buf8
del buf8
buf12 = buf11
del buf11
buf13 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
torch.float32)
triton_per_fused_add_mul_pow_rsqrt_sum_2[grid(16)](buf12, primals_9,
buf10, buf13, 16, 36, XBLOCK=1, num_warps=2, num_stages=1)
buf15 = extern_kernels.convolution(reinterpret_tensor(buf14, (1, 16,
4, 4), (0, 16, 4, 1), 0), reinterpret_tensor(buf13, (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(buf15, (1, 16, 4, 4), (256, 16, 4, 1))
buf16 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
buf17 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf6
triton_poi_fused_add_div_leaky_relu_mul_4[grid(256)](buf15,
primals_10, primals_1, buf16, buf17, 256, XBLOCK=256, num_warps
=4, num_stages=1)
del buf15
del primals_10
return (buf17, primals_4, primals_5, primals_9, buf2, buf4,
reinterpret_tensor(buf5, (16, 4, 3, 3), (36, 9, 3, 1), 0),
reinterpret_tensor(primals_1, (1, 16, 4, 4), (256, 16, 4, 1), 0),
buf7, buf10, buf12, reinterpret_tensor(buf13, (16, 4, 3, 3), (36, 9,
3, 1), 0), reinterpret_tensor(buf14, (1, 16, 4, 4), (256, 16, 4, 1),
0), buf16)
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class FusedLeakyReLU(nn.Module):
def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
super().__init__()
self.bias = nn.Parameter(torch.zeros(channel))
self.negative_slope = negative_slope
self.scale = scale
def forward(self, input):
return fused_leaky_relu(input, self.bias, self.negative_slope, self
.scale)
class StyledConv(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, upsample=False, downsample=False, blur_kernel=[1, 3,
3, 1], demodulate=True):
super().__init__()
self.use_style = use_style
self.conv = ModulatedConv2d(in_channel, out_channel, kernel_size,
style_dim, use_style=use_style, upsample=upsample, downsample=
downsample, blur_kernel=blur_kernel, demodulate=demodulate)
self.activate = FusedLeakyReLU(out_channel)
def forward(self, input, style=None, noise=None):
out = self.conv(input, style)
out = self.activate(out)
return out
class StyledResBlockNew(nn.Module):
def __init__(self, in_channel, style_dim, blur_kernel=[1, 3, 3, 1],
demodulate=True):
super().__init__()
self.conv1 = StyledConv(in_channel, in_channel, 3, style_dim,
upsample=False, blur_kernel=blur_kernel, demodulate=demodulate)
self.conv2 = StyledConv(in_channel, in_channel, 3, style_dim,
upsample=False, blur_kernel=blur_kernel, demodulate=demodulate)
def forward(self, input_0, input_1):
primals_5 = self.conv1.conv.weight
primals_3 = self.conv1.conv.modulation.weight
primals_2 = self.conv1.conv.modulation.bias
primals_6 = self.conv1.activate.bias
primals_9 = self.conv2.conv.weight
primals_4 = self.conv2.conv.modulation.weight
primals_7 = self.conv2.conv.modulation.bias
primals_10 = self.conv2.activate.bias
primals_1 = input_0
primals_8 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9, primals_10])
return output[0]
|
ishine/GANsNRoses
|
StyledResBlock
| false
| 15,629
|
[
"MIT"
] | 969
|
414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
https://github.com/ishine/GANsNRoses/tree/414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
FRM
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class FRM(nn.Module):
def __init__(self, nb_dim, do_add=True, do_mul=True):
super(FRM, self).__init__()
self.fc = nn.Linear(nb_dim, nb_dim)
self.sig = nn.Sigmoid()
self.do_add = do_add
self.do_mul = do_mul
def forward(self, x):
y = F.adaptive_avg_pool1d(x, 1).view(x.size(0), -1)
y = self.sig(self.fc(y)).view(x.size(0), x.size(1), -1)
if self.do_mul:
x = x * y
if self.do_add:
x = x + y
return x
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'nb_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 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
tl.store(out_ptr0 + x0, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_mul_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
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tmp4 = tmp3 + tmp2
tl.store(out_ptr0 + x2, tmp4, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused_mean_0[grid(16)](primals_1, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (4, 4), (4,
1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), alpha
=1, beta=1, out=buf1)
del primals_2
del primals_3
buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_mul_1[grid(64)](primals_1, buf1, buf2, 64,
XBLOCK=64, num_warps=1, num_stages=1)
return buf2, primals_1, reinterpret_tensor(buf0, (4, 4), (4, 1), 0), buf1
class FRMNew(nn.Module):
def __init__(self, nb_dim, do_add=True, do_mul=True):
super(FRMNew, self).__init__()
self.fc = nn.Linear(nb_dim, nb_dim)
self.sig = nn.Sigmoid()
self.do_add = do_add
self.do_mul = do_mul
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]
|
ishine/RawNet
|
FRM
| false
| 15,630
|
[
"MIT"
] | 199
|
cddec5afa27049a4b507f3d48bb02b993ea838bb
|
https://github.com/ishine/RawNet/tree/cddec5afa27049a4b507f3d48bb02b993ea838bb
|
ReCoNet
|
import torch
import numpy as np
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNet(SelectiveLoadModule):
def __init__(self):
super(ReCoNet, self).__init__()
self.style_conv1 = ConvInstRelu(3, 32, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(32, 64, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(64, 128, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(128, 128)
self.style_res2 = ResidualBlock(128, 128)
self.style_res3 = ResidualBlock(128, 128)
self.style_res4 = ResidualBlock(128, 128)
self.style_res5 = ResidualBlock(128, 128)
self.style_deconv1 = UpsampleConvInstRelu(128, 64, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(64, 32, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(32, 3, kernel_size=9, stride=1)
def forward(self, x):
return self.style_deconv3(self.style_deconv2(self.style_deconv1(
self.style_res5(self.style_res4(self.style_res3(self.style_res2
(self.style_res1(self.style_conv3(self.style_conv2(self.
style_conv1(x)))))))))))
def get_inputs():
return [torch.rand([4, 3, 64, 64])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 62208
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_red_fused__native_batch_norm_legit_convolution_1(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr,
RBLOCK: tl.constexpr):
xnumel = 128
rnumel = 4096
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rbase = tl.arange(0, RBLOCK)[None, :]
x3 = xindex
x0 = xindex % 32
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
for roffset in range(0, rnumel, RBLOCK):
rindex = roffset + rbase
rmask = rindex < rnumel
r2 = rindex
tmp0 = tl.load(in_out_ptr0 + (r2 + 4096 * x3), rmask & xmask,
eviction_policy='evict_first', other=0.0)
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers.
welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0)
)
tmp4_mean = tl.where(rmask & xmask, tmp4_mean_next, tmp4_mean)
tmp4_m2 = tl.where(rmask & xmask, tmp4_m2_next, tmp4_m2)
tmp4_weight = tl.where(rmask & xmask, tmp4_weight_next, tmp4_weight)
tl.store(in_out_ptr0 + (r2 + 4096 * x3), tmp2, rmask & xmask)
tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean,
tmp4_m2, tmp4_weight, 1)
tmp4 = tmp4_tmp[:, None]
tmp5 = tmp5_tmp[:, None]
tmp6_tmp[:, None]
tl.store(out_ptr0 + x3, tmp4, xmask)
tmp7 = 4096.0
tmp8 = tmp5 / tmp7
tmp9 = 1e-05
tmp10 = tmp8 + tmp9
tmp11 = libdevice.rsqrt(tmp10)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp11, xmask)
@triton.jit
def triton_poi_fused_repeat_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 128
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 32, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_3(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 557568
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 66
x1 = xindex // 66 % 66
x2 = xindex // 4356
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_4(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 64
tmp0 = tl.load(in_out_ptr0 + (r2 + 1024 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 1024, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 1024.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 1024 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_5(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 % 64, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 295936
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 34
x1 = xindex // 34 % 34
x2 = xindex // 1156
x3 = 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 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr2, out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 128
tmp0 = tl.load(in_ptr0 + x0 % 128, None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x0 % 128, None, eviction_policy='evict_last')
tmp2 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp3 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp5 = tl.broadcast_to(tmp4, [RBLOCK])
tmp7 = tl.broadcast_to(tmp5, [RBLOCK])
tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
tmp10 = tl.full([1], 256, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp5 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [RBLOCK])
tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
tmp18 = 256.0
tmp19 = tmp17 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp4 - tmp12
tmp24 = tmp23 * tmp22
tmp25 = tmp24 * tmp0
tmp26 = tmp25 + tmp1
tmp27 = tl.full([1], 0, tl.int32)
tmp28 = triton_helpers.maximum(tmp27, tmp26)
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(out_ptr1 + x0, tmp1, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp4, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x0, tmp22, None)
tl.store(out_ptr3 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr2 + x0, tmp12, None)
@triton.jit
def triton_poi_fused_reflection_pad2d_8(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 % 18
x1 = xindex // 18 % 18
x2 = xindex // 324
x3 = 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 * x2),
None, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, None)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_9(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_10(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
tmp0 = tl.load(in_ptr0 + x0 % 128, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_11(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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
x2 = xindex // 324
x3 = 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 * x2),
None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, None, 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)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 128
tmp0 = tl.load(in_ptr0 + x0 % 128, None, eviction_policy='evict_last')
tmp1 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp2 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last')
tmp25 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp27 = tl.load(in_out_ptr1 + (r3 + 256 * x0), None)
tmp3 = tmp1 + tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = tl.broadcast_to(tmp4, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = tl.full([1], 256, tl.int32)
tmp10 = tmp9.to(tl.float32)
tmp11 = tmp8 / tmp10
tmp12 = tmp4 - tmp11
tmp13 = tmp12 * tmp12
tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
tmp17 = tmp3 - tmp11
tmp18 = 256.0
tmp19 = tmp16 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp17 * tmp22
tmp24 = tmp23 * tmp0
tmp26 = tmp24 + tmp25
tmp28 = tmp26 + tmp27
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp3, None)
tl.store(in_out_ptr1 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr3 + x0, tmp22, None)
tl.store(out_ptr1 + x0, tmp11, None)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_13(in_out_ptr0,
in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.store(out_ptr2 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
tl.store(out_ptr1 + x3, tmp15, None)
@triton.jit
def triton_poi_fused_arange_14(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
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_15(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_add_reflection_pad2d_16(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
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 + x4, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr3 + x4, None, eviction_policy='evict_last')
tmp19 = tl.load(in_ptr4 + x4, None, eviction_policy='evict_last')
tmp21 = tl.load(in_ptr5 + 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
tmp13 = 256.0
tmp14 = tmp12 / tmp13
tmp15 = 1e-05
tmp16 = tmp14 + tmp15
tmp17 = libdevice.rsqrt(tmp16)
tmp18 = tmp11 * tmp17
tmp20 = tmp18 * tmp19
tmp22 = tmp20 + tmp21
tmp23 = tl.load(in_ptr6 + (tmp8 + 16 * tmp4 + 256 * x4), None,
eviction_policy='evict_last')
tmp24 = tmp22 + tmp23
tl.store(out_ptr0 + x7, tmp24, None)
@triton.jit
def triton_poi_fused_arange_17(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_18(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_reflection_pad2d_relu_19(in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, 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
x2 = xindex // 4356
x5 = 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')
tmp12 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last')
tmp16 = tl.load(in_ptr5 + 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 * x2), xmask,
eviction_policy='evict_last')
tmp11 = tmp9 - tmp10
tmp13 = tmp11 * tmp12
tmp15 = tmp13 * tmp14
tmp17 = tmp15 + tmp16
tmp18 = tl.full([1], 0, tl.int32)
tmp19 = triton_helpers.maximum(tmp18, tmp17)
tl.store(out_ptr0 + x5, tmp19, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_20(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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 % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, None, 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)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_add_convolution_div_mul_tanh_21(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x3 = xindex
x1 = xindex // 4096 % 3
tmp0 = tl.load(in_out_ptr0 + x3, None)
tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.00392156862745098
tmp4 = tmp2 * tmp3
tmp5 = libdevice.tanh(tmp4)
tmp6 = 150.0
tmp7 = tmp5 * tmp6
tmp8 = 127.5
tmp9 = tmp7 + tmp8
tl.store(in_out_ptr0 + x3, tmp2, None)
tl.store(out_ptr0 + x3, tmp9, None)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17,
primals_18, primals_19, primals_20, primals_21, primals_22,
primals_23, primals_24, primals_25, primals_26, primals_27,
primals_28, primals_29, primals_30, primals_31, primals_32,
primals_33, primals_34, primals_35, primals_36, primals_37,
primals_38, primals_39, primals_40, primals_41, primals_42,
primals_43, primals_44, primals_45, primals_46, primals_47,
primals_48, primals_49, primals_50, primals_51, primals_52,
primals_53, primals_54, primals_55, primals_56, primals_57,
primals_58, primals_59, primals_60, primals_61, primals_62, primals_63
) = args
args.clear()
assert_size_stride(primals_1, (4, 3, 64, 64), (12288, 4096, 64, 1))
assert_size_stride(primals_2, (32, 3, 9, 9), (243, 81, 9, 1))
assert_size_stride(primals_3, (32,), (1,))
assert_size_stride(primals_4, (32,), (1,))
assert_size_stride(primals_5, (32,), (1,))
assert_size_stride(primals_6, (64, 32, 3, 3), (288, 9, 3, 1))
assert_size_stride(primals_7, (64,), (1,))
assert_size_stride(primals_8, (64,), (1,))
assert_size_stride(primals_9, (64,), (1,))
assert_size_stride(primals_10, (128, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_11, (128,), (1,))
assert_size_stride(primals_12, (128,), (1,))
assert_size_stride(primals_13, (128,), (1,))
assert_size_stride(primals_14, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_15, (128,), (1,))
assert_size_stride(primals_16, (128,), (1,))
assert_size_stride(primals_17, (128,), (1,))
assert_size_stride(primals_18, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_19, (128,), (1,))
assert_size_stride(primals_20, (128,), (1,))
assert_size_stride(primals_21, (128,), (1,))
assert_size_stride(primals_22, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_23, (128,), (1,))
assert_size_stride(primals_24, (128,), (1,))
assert_size_stride(primals_25, (128,), (1,))
assert_size_stride(primals_26, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_27, (128,), (1,))
assert_size_stride(primals_28, (128,), (1,))
assert_size_stride(primals_29, (128,), (1,))
assert_size_stride(primals_30, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_31, (128,), (1,))
assert_size_stride(primals_32, (128,), (1,))
assert_size_stride(primals_33, (128,), (1,))
assert_size_stride(primals_34, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_35, (128,), (1,))
assert_size_stride(primals_36, (128,), (1,))
assert_size_stride(primals_37, (128,), (1,))
assert_size_stride(primals_38, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_39, (128,), (1,))
assert_size_stride(primals_40, (128,), (1,))
assert_size_stride(primals_41, (128,), (1,))
assert_size_stride(primals_42, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_43, (128,), (1,))
assert_size_stride(primals_44, (128,), (1,))
assert_size_stride(primals_45, (128,), (1,))
assert_size_stride(primals_46, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_47, (128,), (1,))
assert_size_stride(primals_48, (128,), (1,))
assert_size_stride(primals_49, (128,), (1,))
assert_size_stride(primals_50, (128, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_51, (128,), (1,))
assert_size_stride(primals_52, (128,), (1,))
assert_size_stride(primals_53, (128,), (1,))
assert_size_stride(primals_54, (64, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_55, (64,), (1,))
assert_size_stride(primals_56, (64,), (1,))
assert_size_stride(primals_57, (64,), (1,))
assert_size_stride(primals_58, (32, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_59, (32,), (1,))
assert_size_stride(primals_60, (32,), (1,))
assert_size_stride(primals_61, (32,), (1,))
assert_size_stride(primals_62, (3, 32, 9, 9), (2592, 81, 9, 1))
assert_size_stride(primals_63, (3,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 3, 72, 72), (15552, 5184, 72, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_reflection_pad2d_0[grid(62208)](primals_1, buf0,
62208, XBLOCK=256, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 32, 64, 64), (131072, 4096, 64, 1))
buf2 = buf1
del buf1
buf5 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 1, 1), torch.float32
)
buf6 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 128, 128), torch
.float32)
buf8 = reinterpret_tensor(buf6, (1, 128, 1, 1), (128, 1, 1, 1), 0)
del buf6
triton_red_fused__native_batch_norm_legit_convolution_1[grid(128)](buf2
, buf8, primals_3, buf5, 128, 4096, XBLOCK=1, RBLOCK=2048,
num_warps=16, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((128,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(128)](primals_4, buf3, 128, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_4
buf4 = empty_strided_cuda((128,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(128)](primals_5, buf4, 128, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_5
buf9 = empty_strided_cuda((4, 32, 66, 66), (139392, 4356, 66, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_3[grid(557568)](buf2, buf5,
buf8, buf3, buf4, buf9, 557568, XBLOCK=512, num_warps=8,
num_stages=1)
buf10 = extern_kernels.convolution(buf9, primals_6, stride=(2, 2),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf10, (4, 64, 32, 32), (65536, 1024, 32, 1))
buf11 = buf10
del buf10
buf14 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 1, 1), torch.
float32)
buf15 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256),
torch.float32)
buf17 = reinterpret_tensor(buf15, (1, 256, 1, 1), (256, 1, 1, 1), 0)
del buf15
triton_per_fused__native_batch_norm_legit_convolution_4[grid(256)](
buf11, buf17, primals_7, buf14, 256, 1024, num_warps=8,
num_stages=1)
del primals_7
buf12 = empty_strided_cuda((256,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(256)](primals_8, buf12, 256, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_8
buf13 = empty_strided_cuda((256,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(256)](primals_9, buf13, 256, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_9
buf18 = empty_strided_cuda((4, 64, 34, 34), (73984, 1156, 34, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_6[grid(295936)](buf11, buf14,
buf17, buf12, buf13, buf18, 295936, XBLOCK=1024, num_warps=4,
num_stages=1)
buf19 = extern_kernels.convolution(buf18, primals_10, stride=(2, 2),
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))
buf21 = empty_strided_cuda((512,), (1,), torch.float32)
buf22 = empty_strided_cuda((512,), (1,), torch.float32)
buf20 = buf19
del buf19
buf23 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf24 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf26 = reinterpret_tensor(buf24, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf24
buf27 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7[
grid(512)](buf20, buf26, primals_12, primals_13, primals_11,
buf21, buf22, buf23, buf27, 512, 256, num_warps=2, num_stages=1)
del primals_11
del primals_12
del primals_13
buf28 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(165888)](buf27, buf28,
165888, XBLOCK=512, num_warps=8, num_stages=1)
buf29 = extern_kernels.convolution(buf28, primals_14, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf29, (4, 128, 16, 16), (32768, 256, 16, 1))
buf30 = buf29
del buf29
buf33 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf34 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf36 = reinterpret_tensor(buf34, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf34
triton_per_fused__native_batch_norm_legit_convolution_9[grid(512)](
buf30, buf36, primals_15, buf33, 512, 256, num_warps=2,
num_stages=1)
del primals_15
buf31 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_16, buf31, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_16
buf32 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_17, buf32, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_17
buf37 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(165888)](buf30,
buf33, buf36, buf31, buf32, buf37, 165888, XBLOCK=1024,
num_warps=4, num_stages=1)
buf38 = extern_kernels.convolution(buf37, primals_18, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf38, (4, 128, 16, 16), (32768, 256, 16, 1))
buf40 = empty_strided_cuda((512,), (1,), torch.float32)
buf39 = buf38
del buf38
buf41 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf45 = buf27
del buf27
buf44 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(512)](buf39, buf45, primals_20, primals_19, primals_21,
buf40, buf41, buf44, 512, 256, num_warps=2, num_stages=1)
del primals_19
del primals_20
del primals_21
buf46 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(165888)](buf45, buf46,
165888, XBLOCK=512, num_warps=8, num_stages=1)
buf47 = extern_kernels.convolution(buf46, primals_22, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf47, (4, 128, 16, 16), (32768, 256, 16, 1))
buf48 = buf47
del buf47
buf51 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf52 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf54 = reinterpret_tensor(buf52, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf52
triton_per_fused__native_batch_norm_legit_convolution_9[grid(512)](
buf48, buf54, primals_23, buf51, 512, 256, num_warps=2,
num_stages=1)
del primals_23
buf49 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_24, buf49, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_24
buf50 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_25, buf50, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_25
buf55 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(165888)](buf48,
buf51, buf54, buf49, buf50, buf55, 165888, XBLOCK=1024,
num_warps=4, num_stages=1)
buf56 = extern_kernels.convolution(buf55, primals_26, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf56, (4, 128, 16, 16), (32768, 256, 16, 1))
buf58 = empty_strided_cuda((512,), (1,), torch.float32)
buf57 = buf56
del buf56
buf59 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf63 = buf45
del buf45
buf62 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(512)](buf57, buf63, primals_28, primals_27, primals_29,
buf58, buf59, buf62, 512, 256, num_warps=2, num_stages=1)
del primals_27
del primals_28
del primals_29
buf64 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(165888)](buf63, buf64,
165888, XBLOCK=512, num_warps=8, num_stages=1)
buf65 = extern_kernels.convolution(buf64, primals_30, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf65, (4, 128, 16, 16), (32768, 256, 16, 1))
buf66 = buf65
del buf65
buf69 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf70 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf72 = reinterpret_tensor(buf70, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf70
triton_per_fused__native_batch_norm_legit_convolution_9[grid(512)](
buf66, buf72, primals_31, buf69, 512, 256, num_warps=2,
num_stages=1)
del primals_31
buf67 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_32, buf67, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_32
buf68 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_33, buf68, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_33
buf73 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(165888)](buf66,
buf69, buf72, buf67, buf68, buf73, 165888, XBLOCK=1024,
num_warps=4, num_stages=1)
buf74 = extern_kernels.convolution(buf73, primals_34, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf74, (4, 128, 16, 16), (32768, 256, 16, 1))
buf76 = empty_strided_cuda((512,), (1,), torch.float32)
buf75 = buf74
del buf74
buf77 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf81 = buf63
del buf63
buf80 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(512)](buf75, buf81, primals_36, primals_35, primals_37,
buf76, buf77, buf80, 512, 256, num_warps=2, num_stages=1)
del primals_35
del primals_36
del primals_37
buf82 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(165888)](buf81, buf82,
165888, XBLOCK=512, num_warps=8, num_stages=1)
buf83 = extern_kernels.convolution(buf82, primals_38, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf83, (4, 128, 16, 16), (32768, 256, 16, 1))
buf84 = buf83
del buf83
buf87 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf88 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf90 = reinterpret_tensor(buf88, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf88
triton_per_fused__native_batch_norm_legit_convolution_9[grid(512)](
buf84, buf90, primals_39, buf87, 512, 256, num_warps=2,
num_stages=1)
del primals_39
buf85 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_40, buf85, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_40
buf86 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_41, buf86, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_41
buf91 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(165888)](buf84,
buf87, buf90, buf85, buf86, buf91, 165888, XBLOCK=1024,
num_warps=4, num_stages=1)
buf92 = extern_kernels.convolution(buf91, primals_42, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf92, (4, 128, 16, 16), (32768, 256, 16, 1))
buf94 = empty_strided_cuda((512,), (1,), torch.float32)
buf93 = buf92
del buf92
buf95 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf99 = buf81
del buf81
buf98 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(512)](buf93, buf99, primals_44, primals_43, primals_45,
buf94, buf95, buf98, 512, 256, num_warps=2, num_stages=1)
del primals_43
del primals_44
del primals_45
buf100 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(165888)](buf99, buf100,
165888, XBLOCK=512, num_warps=8, num_stages=1)
buf101 = extern_kernels.convolution(buf100, primals_46, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf101, (4, 128, 16, 16), (32768, 256, 16, 1))
buf102 = buf101
del buf101
buf105 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 1, 1), torch.
float32)
buf106 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf108 = reinterpret_tensor(buf106, (1, 512, 1, 1), (512, 1, 1, 1), 0)
del buf106
triton_per_fused__native_batch_norm_legit_convolution_9[grid(512)](
buf102, buf108, primals_47, buf105, 512, 256, num_warps=2,
num_stages=1)
del primals_47
buf103 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_48, buf103, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_48
buf104 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_49, buf104, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_49
buf109 = empty_strided_cuda((4, 128, 18, 18), (41472, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(165888)](buf102,
buf105, buf108, buf103, buf104, buf109, 165888, XBLOCK=1024,
num_warps=4, num_stages=1)
buf110 = extern_kernels.convolution(buf109, primals_50, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf110, (4, 128, 16, 16), (32768, 256, 16, 1))
buf111 = buf110
del buf110
buf113 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf114 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
buf116 = empty_strided_cuda((1, 512, 1, 1), (512, 1, 512, 512),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_13[grid(512)](
buf111, primals_51, buf113, buf114, buf116, 512, 256, num_warps
=2, num_stages=1)
del primals_51
buf112 = empty_strided_cuda((512,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(512)](primals_52, buf112, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_52
buf117 = empty_strided_cuda((32,), (1,), torch.int64)
triton_poi_fused_arange_14[grid(32)](buf117, 32, XBLOCK=32,
num_warps=1, num_stages=1)
buf118 = empty_strided_cuda((32,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_15[grid(32)](buf118, 32,
XBLOCK=32, num_warps=1, num_stages=1)
buf119 = empty_strided_cuda((4, 128, 34, 34), (147968, 1156, 34, 1),
torch.float32)
triton_poi_fused__unsafe_index_add_reflection_pad2d_16[grid(591872)](
buf118, buf111, buf113, buf114, buf112, primals_53, buf99,
buf119, 591872, XBLOCK=512, num_warps=8, num_stages=1)
del buf114
del buf99
del primals_53
buf120 = extern_kernels.convolution(buf119, primals_54, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf120, (4, 64, 32, 32), (65536, 1024, 32, 1))
buf121 = buf120
del buf120
buf124 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 1, 1), torch.
float32)
buf125 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256),
torch.float32)
buf127 = reinterpret_tensor(buf125, (1, 256, 1, 1), (256, 1, 1, 1), 0)
del buf125
triton_per_fused__native_batch_norm_legit_convolution_4[grid(256)](
buf121, buf127, primals_55, buf124, 256, 1024, num_warps=8,
num_stages=1)
del primals_55
buf122 = empty_strided_cuda((256,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(256)](primals_56, buf122, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_56
buf123 = empty_strided_cuda((256,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(256)](primals_57, buf123, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_57
buf128 = empty_strided_cuda((64,), (1,), torch.int64)
triton_poi_fused_arange_17[grid(64)](buf128, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf129 = empty_strided_cuda((64,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_18[grid(64)](buf129, 64,
XBLOCK=64, num_warps=1, num_stages=1)
buf130 = empty_strided_cuda((4, 64, 66, 66), (278784, 4356, 66, 1),
torch.float32)
triton_poi_fused__unsafe_index_reflection_pad2d_relu_19[grid(1115136)](
buf129, buf121, buf124, buf127, buf122, buf123, buf130, 1115136,
XBLOCK=1024, num_warps=4, num_stages=1)
buf131 = extern_kernels.convolution(buf130, primals_58, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf131, (4, 32, 64, 64), (131072, 4096, 64, 1))
buf132 = buf131
del buf131
buf135 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 1, 1), torch.
float32)
buf136 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 128, 128),
torch.float32)
buf138 = reinterpret_tensor(buf136, (1, 128, 1, 1), (128, 1, 1, 1), 0)
del buf136
triton_red_fused__native_batch_norm_legit_convolution_1[grid(128)](
buf132, buf138, primals_59, buf135, 128, 4096, XBLOCK=1, RBLOCK
=2048, num_warps=16, num_stages=1)
del primals_59
buf133 = empty_strided_cuda((128,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(128)](primals_60, buf133, 128,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_60
buf134 = empty_strided_cuda((128,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(128)](primals_61, buf134, 128,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_61
buf139 = empty_strided_cuda((4, 32, 72, 72), (165888, 5184, 72, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_20[grid(663552)](buf132,
buf135, buf138, buf133, buf134, buf139, 663552, XBLOCK=1024,
num_warps=4, num_stages=1)
buf140 = extern_kernels.convolution(buf139, primals_62, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf140, (4, 3, 64, 64), (12288, 4096, 64, 1))
buf141 = buf140
del buf140
buf142 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1),
torch.float32)
triton_poi_fused_add_convolution_div_mul_tanh_21[grid(49152)](buf141,
primals_63, buf142, 49152, XBLOCK=512, num_warps=4, num_stages=1)
del primals_63
return (buf142, primals_2, primals_6, primals_10, primals_14,
primals_18, primals_22, primals_26, primals_30, primals_34,
primals_38, primals_42, primals_46, primals_50, primals_54,
primals_58, primals_62, buf0, buf2, buf3, buf4, buf5, buf8, buf9,
buf11, buf12, buf13, buf14, buf17, buf18, buf20, buf21, buf22,
buf23, buf26, buf28, buf30, buf31, buf32, buf33, buf36, buf37,
buf39, buf40, reinterpret_tensor(buf44, (512,), (1,), 0), buf46,
buf48, buf49, buf50, buf51, buf54, buf55, buf57, buf58,
reinterpret_tensor(buf62, (512,), (1,), 0), buf64, buf66, buf67,
buf68, buf69, buf72, buf73, buf75, buf76, reinterpret_tensor(buf80,
(512,), (1,), 0), buf82, buf84, buf85, buf86, buf87, buf90, buf91,
buf93, buf94, reinterpret_tensor(buf98, (512,), (1,), 0), buf100,
buf102, buf103, buf104, buf105, buf108, buf109, buf111, buf112,
reinterpret_tensor(buf116, (512,), (1,), 0), buf117, buf118, buf119,
buf121, buf122, buf123, buf124, buf127, buf128, buf129, buf130,
buf132, buf133, buf134, buf135, buf138, buf139, buf141,
reinterpret_tensor(buf113, (1, 512, 1, 1), (512, 1, 1, 1), 0),
reinterpret_tensor(buf95, (1, 512, 1, 1), (512, 1, 1, 1), 0),
reinterpret_tensor(buf77, (1, 512, 1, 1), (512, 1, 1, 1), 0),
reinterpret_tensor(buf59, (1, 512, 1, 1), (512, 1, 1, 1), 0),
reinterpret_tensor(buf41, (1, 512, 1, 1), (512, 1, 1, 1), 0))
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNetNew(SelectiveLoadModule):
def __init__(self):
super(ReCoNetNew, self).__init__()
self.style_conv1 = ConvInstRelu(3, 32, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(32, 64, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(64, 128, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(128, 128)
self.style_res2 = ResidualBlock(128, 128)
self.style_res3 = ResidualBlock(128, 128)
self.style_res4 = ResidualBlock(128, 128)
self.style_res5 = ResidualBlock(128, 128)
self.style_deconv1 = UpsampleConvInstRelu(128, 64, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(64, 32, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(32, 3, kernel_size=9, stride=1)
def forward(self, input_0):
primals_2 = self.style_conv1.conv2d.weight
primals_3 = self.style_conv1.conv2d.bias
primals_4 = self.style_conv1.instance.weight
primals_5 = self.style_conv1.instance.bias
primals_6 = self.style_conv2.conv2d.weight
primals_7 = self.style_conv2.conv2d.bias
primals_8 = self.style_conv2.instance.weight
primals_9 = self.style_conv2.instance.bias
primals_10 = self.style_conv3.conv2d.weight
primals_11 = self.style_conv3.conv2d.bias
primals_12 = self.style_conv3.instance.weight
primals_13 = self.style_conv3.instance.bias
primals_14 = self.style_res1.conv1.conv2d.weight
primals_15 = self.style_res1.conv1.conv2d.bias
primals_16 = self.style_res1.in1.weight
primals_17 = self.style_res1.in1.bias
primals_18 = self.style_res1.conv2.conv2d.weight
primals_19 = self.style_res1.conv2.conv2d.bias
primals_20 = self.style_res1.in2.weight
primals_21 = self.style_res1.in2.bias
primals_22 = self.style_res2.conv1.conv2d.weight
primals_23 = self.style_res2.conv1.conv2d.bias
primals_24 = self.style_res2.in1.weight
primals_25 = self.style_res2.in1.bias
primals_26 = self.style_res2.conv2.conv2d.weight
primals_27 = self.style_res2.conv2.conv2d.bias
primals_28 = self.style_res2.in2.weight
primals_29 = self.style_res2.in2.bias
primals_30 = self.style_res3.conv1.conv2d.weight
primals_31 = self.style_res3.conv1.conv2d.bias
primals_32 = self.style_res3.in1.weight
primals_33 = self.style_res3.in1.bias
primals_34 = self.style_res3.conv2.conv2d.weight
primals_35 = self.style_res3.conv2.conv2d.bias
primals_36 = self.style_res3.in2.weight
primals_37 = self.style_res3.in2.bias
primals_38 = self.style_res4.conv1.conv2d.weight
primals_39 = self.style_res4.conv1.conv2d.bias
primals_40 = self.style_res4.in1.weight
primals_41 = self.style_res4.in1.bias
primals_42 = self.style_res4.conv2.conv2d.weight
primals_43 = self.style_res4.conv2.conv2d.bias
primals_44 = self.style_res4.in2.weight
primals_45 = self.style_res4.in2.bias
primals_46 = self.style_res5.conv1.conv2d.weight
primals_47 = self.style_res5.conv1.conv2d.bias
primals_48 = self.style_res5.in1.weight
primals_49 = self.style_res5.in1.bias
primals_50 = self.style_res5.conv2.conv2d.weight
primals_51 = self.style_res5.conv2.conv2d.bias
primals_52 = self.style_res5.in2.weight
primals_53 = self.style_res5.in2.bias
primals_54 = self.style_deconv1.conv2d.weight
primals_55 = self.style_deconv1.conv2d.bias
primals_56 = self.style_deconv1.instance.weight
primals_57 = self.style_deconv1.instance.bias
primals_58 = self.style_deconv2.conv2d.weight
primals_59 = self.style_deconv2.conv2d.bias
primals_60 = self.style_deconv2.instance.weight
primals_61 = self.style_deconv2.instance.bias
primals_62 = self.style_deconv3.conv2d.weight
primals_63 = self.style_deconv3.conv2d.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20, primals_21, primals_22, primals_23, primals_24,
primals_25, primals_26, primals_27, primals_28, primals_29,
primals_30, primals_31, primals_32, primals_33, primals_34,
primals_35, primals_36, primals_37, primals_38, primals_39,
primals_40, primals_41, primals_42, primals_43, primals_44,
primals_45, primals_46, primals_47, primals_48, primals_49,
primals_50, primals_51, primals_52, primals_53, primals_54,
primals_55, primals_56, primals_57, primals_58, primals_59,
primals_60, primals_61, primals_62, primals_63])
return output[0]
|
irsisyphus/reconet
|
ReCoNet
| false
| 15,631
|
[
"MIT"
] | 56
|
863acf8dde4d45c8521634af27878fe04f3b2e56
|
https://github.com/irsisyphus/reconet/tree/863acf8dde4d45c8521634af27878fe04f3b2e56
|
AttDistance
|
import torch
import torch.nn.functional as F
class AttDistance(torch.nn.Module):
"""
AttDistance: Distance attention that can be used by the Alignment module.
"""
def __init__(self, dist_norm=1, weight_norm=1):
super().__init__()
self.dist_norm = dist_norm
self.weight_norm = weight_norm
def forward(self, query, y):
att = (query.unsqueeze(1) - y.unsqueeze(2)).abs().pow(self.dist_norm)
att = att.mean(dim=3).pow(self.weight_norm)
att = -att.transpose(2, 1)
sim = att.max(2)[0].unsqueeze(1)
att = F.softmax(att, dim=2)
return att, sim
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_neg_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x4 = xindex // 16
x1 = xindex // 4 % 4
x3 = xindex // 64
x5 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (4 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp5 = tl.load(in_ptr1 + (4 + x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp9 = tl.load(in_ptr0 + (8 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr1 + (8 + x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp14 = tl.load(in_ptr0 + (12 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp15 = tl.load(in_ptr1 + (12 + x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp3 = tl_math.abs(tmp2)
tmp6 = tmp4 - tmp5
tmp7 = tl_math.abs(tmp6)
tmp8 = tmp3 + tmp7
tmp11 = tmp9 - tmp10
tmp12 = tl_math.abs(tmp11)
tmp13 = tmp8 + tmp12
tmp16 = tmp14 - tmp15
tmp17 = tl_math.abs(tmp16)
tmp18 = tmp13 + tmp17
tmp19 = 4.0
tmp20 = tmp18 / tmp19
tmp21 = -tmp20
tl.store(out_ptr0 + x5, tmp21, 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 % 4
x2 = xindex // 16
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 4
x2 = xindex // 16
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
@triton.jit
def triton_poi_fused_max_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 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
tmp2 = triton_helpers.maximum(tmp0, tmp1)
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp6 = triton_helpers.maximum(tmp4, tmp5)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
arg0_1, 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_neg_0[grid(256)](arg0_1, arg1_1, buf0, 256, XBLOCK
=256, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_2[grid(256)](buf1, buf2, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf1
buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_max_3[grid(64)](buf0, buf3, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf0
return buf2, reinterpret_tensor(buf3, (4, 1, 4, 4), (16, 16, 4, 1), 0)
class AttDistanceNew(torch.nn.Module):
"""
AttDistance: Distance attention that can be used by the Alignment module.
"""
def __init__(self, dist_norm=1, weight_norm=1):
super().__init__()
self.dist_norm = dist_norm
self.weight_norm = weight_norm
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0], output[1]
|
ishine/NISQA
|
AttDistance
| false
| 15,632
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
AFMS
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AFMS(nn.Module):
"""
Alpha-Feature map scaling, added to the output of each residual block[1,2].
Reference:
[1] RawNet2 : https://www.isca-speech.org/archive/Interspeech_2020/pdfs/1011.pdf
[2] AMFS : https://www.koreascience.or.kr/article/JAKO202029757857763.page
"""
def __init__(self, nb_dim):
super(AFMS, self).__init__()
self.alpha = nn.Parameter(torch.ones((nb_dim, 1)))
self.fc = nn.Linear(nb_dim, nb_dim)
self.sig = nn.Sigmoid()
def forward(self, x):
y = F.adaptive_avg_pool1d(x, 1).view(x.size(0), -1)
y = self.sig(self.fc(y)).view(x.size(0), x.size(1), -1)
x = x + self.alpha
x = x * y
return x
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'nb_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 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
tl.store(out_ptr0 + x0, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_mul_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 4 % 4
x4 = xindex // 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tl.sigmoid(tmp3)
tmp5 = tmp2 * tmp4
tl.store(out_ptr0 + x3, tmp5, 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, 1, 1), (4, 1, 16, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused_mean_0[grid(16)](primals_1, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (4, 4), (4,
1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), alpha
=1, beta=1, out=buf1)
del primals_2
del primals_3
buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_mul_1[grid(64)](primals_1, primals_4, buf1,
buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
return buf2, primals_1, primals_4, reinterpret_tensor(buf0, (4, 4), (4,
1), 0), buf1
class AFMSNew(nn.Module):
"""
Alpha-Feature map scaling, added to the output of each residual block[1,2].
Reference:
[1] RawNet2 : https://www.isca-speech.org/archive/Interspeech_2020/pdfs/1011.pdf
[2] AMFS : https://www.koreascience.or.kr/article/JAKO202029757857763.page
"""
def __init__(self, nb_dim):
super(AFMSNew, self).__init__()
self.alpha = nn.Parameter(torch.ones((nb_dim, 1)))
self.fc = nn.Linear(nb_dim, nb_dim)
self.sig = nn.Sigmoid()
def forward(self, input_0):
primals_4 = self.alpha
primals_2 = self.fc.weight
primals_3 = self.fc.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
ishine/RawNet
|
AFMS
| false
| 15,633
|
[
"MIT"
] | 199
|
cddec5afa27049a4b507f3d48bb02b993ea838bb
|
https://github.com/ishine/RawNet/tree/cddec5afa27049a4b507f3d48bb02b993ea838bb
|
ToRGB
|
import math
import torch
from torch import nn
from torch.nn import functional as F
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Upsample(nn.Module):
def __init__(self, kernel, factor=2):
super().__init__()
self.factor = factor
kernel = make_kernel(kernel) * factor ** 2
self.register_buffer('kernel', kernel)
p = kernel.shape[0] - factor
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2
self.pad = pad0, pad1
def forward(self, input):
out = upfirdn2d(input, self.kernel, up=self.factor, down=1, pad=
self.pad)
return out
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class ToRGB(nn.Module):
def __init__(self, in_channel, style_dim, upsample=True, blur_kernel=[1,
3, 3, 1]):
super().__init__()
if upsample:
self.upsample = Upsample(blur_kernel)
self.conv = ModulatedConv2d(in_channel, 3, 1, style_dim, demodulate
=False)
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
def forward(self, input, style, skip=None):
out = self.conv(input, style)
out = out + self.bias
if skip is not None:
skip = self.upsample(skip)
out = out + skip
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4, 'style_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import math
from torch import nn
from torch.nn import functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_mul_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_mul_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex % 12
x0 = xindex % 4
x2 = xindex // 12
x4 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tmp4 = tmp2 * tmp3
tl.store(out_ptr0 + x4, tmp4, xmask)
@triton.jit
def triton_poi_fused_add_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 192
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 3
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (1, 3, 4, 1, 1), (12, 4, 1, 1, 1))
assert_size_stride(primals_6, (1, 3, 1, 1), (3, 1, 1, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](primals_3, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_3
buf1 = empty_strided_cuda((4,), (1,), torch.float32)
triton_poi_fused_mul_1[grid(4)](primals_2, buf1, 4, XBLOCK=4,
num_warps=1, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(buf1, primals_4, reinterpret_tensor(buf0, (4,
4), (1, 4), 0), alpha=1, beta=1, out=buf2)
del buf0
del buf1
buf3 = empty_strided_cuda((4, 3, 4, 1, 1), (12, 4, 1, 1, 1), torch.
float32)
triton_poi_fused_mul_2[grid(48)](primals_5, buf2, buf3, 48, XBLOCK=
64, num_warps=1, num_stages=1)
buf4 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf3, (12, 4,
1, 1), (4, 1, 0, 0), 0), stride=(1, 1), padding=(0, 0),
dilation=(1, 1), transposed=False, output_padding=(0, 0),
groups=4, bias=None)
assert_size_stride(buf4, (1, 12, 4, 4), (192, 16, 4, 1))
buf5 = reinterpret_tensor(buf4, (4, 3, 4, 4), (48, 16, 4, 1), 0)
del buf4
triton_poi_fused_add_3[grid(192)](buf5, primals_6, 192, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_6
return buf5, primals_4, primals_5, buf2, reinterpret_tensor(buf3, (12,
4, 1, 1), (4, 1, 1, 1), 0), reinterpret_tensor(primals_1, (1, 16, 4,
4), (256, 16, 4, 1), 0)
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Upsample(nn.Module):
def __init__(self, kernel, factor=2):
super().__init__()
self.factor = factor
kernel = make_kernel(kernel) * factor ** 2
self.register_buffer('kernel', kernel)
p = kernel.shape[0] - factor
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2
self.pad = pad0, pad1
def forward(self, input):
out = upfirdn2d(input, self.kernel, up=self.factor, down=1, pad=
self.pad)
return out
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class ToRGBNew(nn.Module):
def __init__(self, in_channel, style_dim, upsample=True, blur_kernel=[1,
3, 3, 1]):
super().__init__()
if upsample:
self.upsample = Upsample(blur_kernel)
self.conv = ModulatedConv2d(in_channel, 3, 1, style_dim, demodulate
=False)
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
def forward(self, input_0, input_1):
primals_6 = self.bias
primals_5 = self.conv.weight
primals_3 = self.conv.modulation.weight
primals_2 = self.conv.modulation.bias
primals_1 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0]
|
ishine/GANsNRoses
|
ToRGB
| false
| 15,634
|
[
"MIT"
] | 969
|
414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
https://github.com/ishine/GANsNRoses/tree/414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
EmbedNet
|
from _paritybench_helpers import _mock_config
import torch
from torchvision.transforms import functional as F
import torch.utils.data
from torch import nn
import torch.nn.functional as F
class EmbedNet(nn.Module):
def __init__(self, cfg):
super(EmbedNet, self).__init__()
self.embed_conv1 = nn.Conv2d(1024, 512, kernel_size=1, stride=1)
self.embed_conv2 = nn.Conv2d(512, 512, kernel_size=3, stride=1,
padding=1)
self.embed_conv3 = nn.Conv2d(512, 2048, kernel_size=1, stride=1)
for l in [self.embed_conv1, self.embed_conv2, self.embed_conv3]:
nn.init.kaiming_uniform_(l.weight, a=1)
nn.init.zeros_(l.bias)
def forward(self, x):
x = F.relu(self.embed_conv1(x))
x = F.relu(self.embed_conv2(x))
x = self.embed_conv3(x)
return x
def get_inputs():
return [torch.rand([4, 1024, 64, 64])]
def get_init_inputs():
return [[], {'cfg': _mock_config()}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.utils.data
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
x2 = xindex
y3 = yindex
y0 = yindex % 1024
y1 = yindex // 1024
tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), None)
tl.store(out_ptr0 + (y0 + 1024 * x2 + 4194304 * y1), tmp0, None)
@triton.jit
def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1)
) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 512
y1 = yindex // 512
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 512 * x2 + 4608 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 512
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_convolution_3(in_ptr0, in_ptr1, out_ptr0, ynumel,
xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
x2 = xindex
y0 = yindex % 2048
y1 = yindex // 2048
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 2048 * x2 + 8388608 * y1), None,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(out_ptr0 + (x2 + 4096 * y3), tmp2, 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, (512, 1024, 1, 1), (1024, 1, 1, 1))
assert_size_stride(primals_2, (512,), (1,))
assert_size_stride(primals_3, (4, 1024, 64, 64), (4194304, 4096, 64, 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, (2048, 512, 1, 1), (512, 1, 1, 1))
assert_size_stride(primals_7, (2048,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 1024, 64, 64), (4194304, 1, 65536,
1024), torch.float32)
get_raw_stream(0)
triton_poi_fused_0[grid(4096, 4096)](primals_3, buf0, 4096, 4096,
XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
del primals_3
buf1 = empty_strided_cuda((512, 512, 3, 3), (4608, 1, 1536, 512),
torch.float32)
triton_poi_fused_1[grid(262144, 9)](primals_4, buf1, 262144, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_4
buf2 = 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(buf2, (4, 512, 64, 64), (2097152, 1, 32768, 512))
buf3 = buf2
del buf2
triton_poi_fused_convolution_relu_2[grid(8388608)](buf3, primals_2,
8388608, XBLOCK=512, num_warps=8, num_stages=1)
del primals_2
buf4 = extern_kernels.convolution(buf3, buf1, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf4, (4, 512, 64, 64), (2097152, 1, 32768, 512))
buf5 = buf4
del buf4
triton_poi_fused_convolution_relu_2[grid(8388608)](buf5, primals_5,
8388608, XBLOCK=512, num_warps=8, num_stages=1)
del primals_5
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, 2048, 64, 64), (8388608, 1, 131072, 2048))
buf7 = empty_strided_cuda((4, 2048, 64, 64), (8388608, 4096, 64, 1),
torch.float32)
triton_poi_fused_convolution_3[grid(8192, 4096)](buf6, primals_7,
buf7, 8192, 4096, XBLOCK=64, YBLOCK=64, num_warps=8, num_stages=1)
del buf6
del primals_7
return buf7, primals_1, buf0, buf1, primals_6, buf3, buf5
class EmbedNetNew(nn.Module):
def __init__(self, cfg):
super(EmbedNetNew, self).__init__()
self.embed_conv1 = nn.Conv2d(1024, 512, kernel_size=1, stride=1)
self.embed_conv2 = nn.Conv2d(512, 512, kernel_size=3, stride=1,
padding=1)
self.embed_conv3 = nn.Conv2d(512, 2048, kernel_size=1, stride=1)
for l in [self.embed_conv1, self.embed_conv2, self.embed_conv3]:
nn.init.kaiming_uniform_(l.weight, a=1)
nn.init.zeros_(l.bias)
def forward(self, input_0):
primals_1 = self.embed_conv1.weight
primals_2 = self.embed_conv1.bias
primals_4 = self.embed_conv2.weight
primals_5 = self.embed_conv2.bias
primals_6 = self.embed_conv3.weight
primals_7 = self.embed_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]
|
hanranCode/mega.pytorch
|
EmbedNet
| false
| 15,635
|
[
"BSD-2-Clause"
] | 521
|
28c8a184372aa57a942576a944b3526590bc1ace
|
https://github.com/hanranCode/mega.pytorch/tree/28c8a184372aa57a942576a944b3526590bc1ace
|
TransitionModel
|
import torch
from torch import nn
def log_clamped(x, eps=0.0001):
clamped_x = torch.clamp(x, min=eps)
return torch.log(clamped_x)
def logsumexp(x, dim):
"""
Differentiable LogSumExp: Does not creates nan gradients when all the inputs are -inf
Args:
x : torch.Tensor - The input tensor
dim: int - The dimension on which the log sum exp has to be applied
"""
m, _ = x.max(dim=dim)
mask = m == -float('inf')
s = (x - m.masked_fill_(mask, 0).unsqueeze(dim=dim)).exp().sum(dim=dim)
return s.masked_fill_(mask, 1).log() + m.masked_fill_(mask, -float('inf'))
class TransitionModel(nn.Module):
"""
Transition Model of the HMM, it represents the probability of transitioning form current state to all other states
"""
def __init__(self):
super(TransitionModel, self).__init__()
def set_staying_and_transitioning_probability(self, staying, transitioning
):
"""
Make reference of the staying and transitioning probabilities as instance parameters of class
"""
self.staying_probability = staying
self.transition_probability = transitioning
def forward(self, log_alpha_scaled, transition_vector, state_lengths):
"""
It is the product of the past state with transitional probabilities
and since it is in log scale, the product will be converted to logsumexp
Args:
log_alpha_scaled (torch.Tensor): Multiply previous timestep's alphas by transition matrix (in log domain)
shape: (batch size, N)
transition_vector (torch.tensor): transition vector for each state
shape: (N)
state_lengths (int tensor): Lengths of states in a batch
shape: (batch)
"""
T_max = log_alpha_scaled.shape[1]
transition_probability = torch.sigmoid(transition_vector)
staying_probability = torch.sigmoid(-transition_vector)
self.set_staying_and_transitioning_probability(staying_probability,
transition_probability)
log_staying_probability = log_clamped(staying_probability)
log_transition_probability = log_clamped(transition_probability)
staying = log_alpha_scaled + log_staying_probability
leaving = log_alpha_scaled + log_transition_probability
leaving = leaving.roll(1, dims=1)
leaving[:, 0] = -float('inf')
mask_tensor = log_alpha_scaled.new_zeros(T_max)
not_state_lengths_mask = ~(torch.arange(T_max, out=mask_tensor).
expand(len(state_lengths), T_max) < state_lengths.unsqueeze(1))
out = logsumexp(torch.stack((staying, leaving), dim=2), dim=2)
out = out.masked_fill(not_state_lengths_mask, -float('inf'))
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_neg_sigmoid_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 = -tmp0
tmp2 = tl.sigmoid(tmp1)
tmp3 = tl.sigmoid(tmp0)
tl.store(out_ptr0 + x0, tmp2, xmask)
tl.store(out_ptr1 + x0, tmp3, xmask)
@triton.jit
def triton_poi_fused_stack_1(in_ptr0, in_ptr1, in_ptr2, 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
x4 = xindex // 32
x2 = xindex // 32 % 4
x3 = xindex // 128
x5 = xindex
tmp0 = x1
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x4), tmp4 & xmask, other=0.0)
tmp6 = tl.load(in_ptr1 + (x0 + 4 * x1 + 16 * x4), tmp4 & xmask, other=0.0)
tmp7 = 0.0001
tmp8 = triton_helpers.maximum(tmp6, tmp7)
tmp9 = tl_math.log(tmp8)
tmp10 = tmp5 + tmp9
tmp11 = tl.full(tmp10.shape, 0.0, tmp10.dtype)
tmp12 = tl.where(tmp4, tmp10, tmp11)
tmp13 = tmp0 >= tmp3
tl.full([1], 8, tl.int64)
tmp16 = x2
tmp17 = tl.full([1], 0, tl.int32)
tmp18 = tmp16 == tmp17
tmp19 = tl.load(in_ptr0 + (x0 + 4 * (-4 + x1) + 16 * ((3 + x2) % 4) +
64 * x3), tmp13 & xmask, other=0.0)
tmp20 = tl.load(in_ptr2 + (x0 + 4 * (-4 + x1) + 16 * ((3 + x2) % 4) +
64 * x3), tmp13 & xmask, other=0.0)
tmp21 = triton_helpers.maximum(tmp20, tmp7)
tmp22 = tl_math.log(tmp21)
tmp23 = tmp19 + tmp22
tmp24 = float('-inf')
tmp25 = tl.where(tmp18, tmp24, tmp23)
tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
tmp27 = tl.where(tmp13, tmp25, tmp26)
tmp28 = tl.where(tmp4, tmp12, tmp27)
tl.store(out_ptr0 + x5, tmp28, xmask)
@triton.jit
def triton_poi_fused_add_bitwise_not_eq_exp_log_lt_masked_fill_max_sub_sum_2(
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
x4 = xindex // 256
x5 = xindex % 64
x0 = xindex % 4
x6 = xindex % 16
x7 = xindex // 16 % 16
x8 = xindex
tmp0 = tl.load(in_ptr0 + (x5 + 64 * x4), xmask, eviction_policy=
'evict_last')
tmp5 = tl.load(in_ptr1 + (x6 + 32 * x7), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr1 + (16 + x6 + 32 * x7), xmask, eviction_policy=
'evict_last')
tmp1 = x0
tmp2 = tmp1.to(tl.float32)
tmp3 = tmp2 < tmp0
tmp4 = tmp3 == 0
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = float('-inf')
tmp9 = tmp7 == tmp8
tmp10 = 0.0
tmp11 = tl.where(tmp9, tmp10, tmp7)
tmp12 = tmp5 - tmp11
tmp13 = tl_math.exp(tmp12)
tmp14 = tmp6 - tmp11
tmp15 = tl_math.exp(tmp14)
tmp16 = tmp13 + tmp15
tmp17 = 1.0
tmp18 = tl.where(tmp9, tmp17, tmp16)
tmp19 = tl_math.log(tmp18)
tmp20 = tl.where(tmp9, tmp8, tmp11)
tmp21 = tmp19 + tmp20
tmp22 = tl.where(tmp4, tmp8, tmp21)
tl.store(out_ptr0 + x8, tmp22, 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)
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_neg_sigmoid_0[grid(256)](arg1_1, buf0, buf1, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del arg1_1
buf2 = empty_strided_cuda((4, 4, 8, 4), (128, 32, 4, 1), torch.float32)
triton_poi_fused_stack_1[grid(512)](arg0_1, buf0, buf1, buf2, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_add_bitwise_not_eq_exp_log_lt_masked_fill_max_sub_sum_2[
grid(1024)](arg2_1, buf2, buf3, 1024, XBLOCK=256, num_warps=4,
num_stages=1)
del arg2_1
del buf2
return buf3, buf1, buf0
def log_clamped(x, eps=0.0001):
clamped_x = torch.clamp(x, min=eps)
return torch.log(clamped_x)
def logsumexp(x, dim):
"""
Differentiable LogSumExp: Does not creates nan gradients when all the inputs are -inf
Args:
x : torch.Tensor - The input tensor
dim: int - The dimension on which the log sum exp has to be applied
"""
m, _ = x.max(dim=dim)
mask = m == -float('inf')
s = (x - m.masked_fill_(mask, 0).unsqueeze(dim=dim)).exp().sum(dim=dim)
return s.masked_fill_(mask, 1).log() + m.masked_fill_(mask, -float('inf'))
class TransitionModelNew(nn.Module):
"""
Transition Model of the HMM, it represents the probability of transitioning form current state to all other states
"""
def __init__(self):
super(TransitionModelNew, self).__init__()
def set_staying_and_transitioning_probability(self, staying, transitioning
):
"""
Make reference of the staying and transitioning probabilities as instance parameters of class
"""
self.staying_probability = staying
self.transition_probability = transitioning
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]
|
ishine/Neural-HMM
|
TransitionModel
| false
| 15,636
|
[
"MIT"
] | 66
|
c0bc23ab88f831173d2d4db29a84503b80c5cdc4
|
https://github.com/ishine/Neural-HMM/tree/c0bc23ab88f831173d2d4db29a84503b80c5cdc4
|
AttentiveStatsPool
|
import torch
import torch.nn
import torch.nn as nn
class AttentiveStatsPool(nn.Module):
def __init__(self, in_dim, bottleneck_dim):
super().__init__()
self.linear1 = nn.Conv1d(in_dim, bottleneck_dim, kernel_size=1)
self.linear2 = nn.Conv1d(bottleneck_dim, in_dim, kernel_size=1)
def forward(self, x):
alpha = torch.tanh(self.linear1(x))
alpha = torch.softmax(self.linear2(alpha), dim=2)
mean = torch.sum(alpha * x, dim=2)
residuals = torch.sum(alpha * x ** 2, dim=2) - mean ** 2
std = torch.sqrt(residuals.clamp(min=1e-09))
return torch.cat([mean, std], dim=1)
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'in_dim': 4, 'bottleneck_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
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_tanh_0(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 4 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(in_out_ptr0 + x3, tmp3, 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_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 = 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 = 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_clamp_mul_pow_sqrt_sub_sum_4(in_ptr0, in_ptr1,
out_ptr0, out_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x2, 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 * x2), 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 * x2), 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 * x2), 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 = tmp1 * tmp1
tmp16 = tmp0 * tmp15
tmp17 = tmp4 * tmp4
tmp18 = tmp3 * tmp17
tmp19 = tmp16 + tmp18
tmp20 = tmp8 * tmp8
tmp21 = tmp7 * tmp20
tmp22 = tmp19 + tmp21
tmp23 = tmp12 * tmp12
tmp24 = tmp11 * tmp23
tmp25 = tmp22 + tmp24
tmp26 = tmp14 * tmp14
tmp27 = tmp25 - tmp26
tmp28 = 1e-09
tmp29 = triton_helpers.maximum(tmp27, tmp28)
tmp30 = libdevice.sqrt(tmp29)
tl.store(out_ptr0 + (x0 + 8 * x1), tmp14, xmask)
tl.store(out_ptr2 + (x0 + 8 * x1), tmp30, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 1), (4, 1, 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, 1), (4, 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,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 4), (16, 4, 1))
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_convolution_tanh_0[grid(64)](buf1, primals_2, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_2
buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf2, (4, 4, 4), (16, 4, 1))
buf3 = buf2
del buf2
triton_poi_fused_convolution_1[grid(64)](buf3, primals_5, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_5
buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_2[grid(64)](buf3, buf4, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_3[grid(64)](buf4, buf5, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf4
buf9 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
buf6 = reinterpret_tensor(buf9, (4, 4), (8, 1), 0)
buf8 = reinterpret_tensor(buf9, (4, 4), (8, 1), 4)
triton_poi_fused_clamp_mul_pow_sqrt_sub_sum_4[grid(16)](buf5,
primals_3, buf6, buf8, 16, XBLOCK=16, num_warps=1, num_stages=1)
del buf5
return buf9, primals_1, primals_3, primals_4, buf1, buf3
class AttentiveStatsPoolNew(nn.Module):
def __init__(self, in_dim, bottleneck_dim):
super().__init__()
self.linear1 = nn.Conv1d(in_dim, bottleneck_dim, kernel_size=1)
self.linear2 = nn.Conv1d(bottleneck_dim, in_dim, kernel_size=1)
def forward(self, input_0):
primals_1 = self.linear1.weight
primals_2 = self.linear1.bias
primals_4 = self.linear2.weight
primals_5 = self.linear2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ishine/asv-subtools
|
AttentiveStatsPool
| false
| 15,637
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
InResBlock
|
import math
import torch
from torch import nn
from torch.nn import functional as F
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class FusedLeakyReLU(nn.Module):
def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
super().__init__()
self.bias = nn.Parameter(torch.zeros(channel))
self.negative_slope = negative_slope
self.scale = scale
def forward(self, input):
return fused_leaky_relu(input, self.bias, self.negative_slope, self
.scale)
class StyledConv(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, upsample=False, downsample=False, blur_kernel=[1, 3,
3, 1], demodulate=True):
super().__init__()
self.use_style = use_style
self.conv = ModulatedConv2d(in_channel, out_channel, kernel_size,
style_dim, use_style=use_style, upsample=upsample, downsample=
downsample, blur_kernel=blur_kernel, demodulate=demodulate)
self.activate = FusedLeakyReLU(out_channel)
def forward(self, input, style=None, noise=None):
out = self.conv(input, style)
out = self.activate(out)
return out
class InResBlock(nn.Module):
def __init__(self, in_channel, blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.conv1 = StyledConv(in_channel, in_channel, 3, None,
blur_kernel=blur_kernel, demodulate=True, use_style=False)
self.conv2 = StyledConv(in_channel, in_channel, 3, None,
blur_kernel=blur_kernel, demodulate=True, use_style=False)
def forward(self, input):
out = self.conv1(input, None)
out = self.conv2(out, None)
out = (out + input) / math.sqrt(2)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channel': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import math
from torch import nn
from torch.nn import functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_add_mul_pow_rsqrt_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
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
r5 = rindex
x0 = xindex % 4
r3 = rindex // 9
x4 = xindex
tmp0 = tl.load(in_ptr0 + (r5 + 36 * x0), rmask & xmask, eviction_policy
='evict_last', other=0.0)
tmp3 = tl.load(in_ptr1 + r3, rmask, eviction_policy='evict_last', other=0.0
)
tmp1 = 0.16666666666666666
tmp2 = tmp0 * tmp1
tmp4 = tmp2 * tmp3
tmp5 = tmp4 * tmp4
tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
tmp8 = tl.where(rmask & xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = 1e-08
tmp11 = tmp9 + tmp10
tmp12 = libdevice.rsqrt(tmp11)
tmp13 = tmp4 * tmp12
tl.debug_barrier()
tl.store(in_out_ptr0 + x4, tmp12, xmask)
tl.store(out_ptr0 + (r5 + 36 * x4), tmp13, rmask & xmask)
@triton.jit
def triton_poi_fused_add_leaky_relu_mul_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.2
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tmp8 = 1.4142135623730951
tmp9 = tmp7 * tmp8
tl.store(out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr1 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused_add_div_leaky_relu_mul_2(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr2 + x3, xmask)
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.2
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tmp8 = 1.4142135623730951
tmp9 = tmp7 * tmp8
tmp11 = tmp9 + tmp10
tmp12 = 0.7071067811865475
tmp13 = tmp11 * tmp12
tl.store(out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr1 + x3, tmp13, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
assert_size_stride(primals_3, (1, 1, 4, 1, 1), (4, 4, 1, 1, 1))
assert_size_stride(primals_4, (4,), (1,))
assert_size_stride(primals_5, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
assert_size_stride(primals_6, (1, 1, 4, 1, 1), (4, 4, 1, 1, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf1 = buf0
del buf0
buf2 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
torch.float32)
get_raw_stream(0)
triton_per_fused_add_mul_pow_rsqrt_sum_0[grid(16)](buf1, primals_2,
primals_3, buf2, 16, 36, XBLOCK=8, num_warps=4, num_stages=1)
buf3 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf2, (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(buf3, (1, 16, 4, 4), (256, 16, 4, 1))
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_leaky_relu_mul_1[grid(256)](buf3, primals_4,
buf4, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_4
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf6 = buf5
del buf5
buf7 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
torch.float32)
triton_per_fused_add_mul_pow_rsqrt_sum_0[grid(16)](buf6, primals_5,
primals_6, buf7, 16, 36, XBLOCK=8, num_warps=4, num_stages=1)
buf9 = extern_kernels.convolution(reinterpret_tensor(buf8, (1, 16,
4, 4), (0, 16, 4, 1), 0), reinterpret_tensor(buf7, (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))
buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
buf11 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf3
triton_poi_fused_add_div_leaky_relu_mul_2[grid(256)](buf9,
primals_7, primals_1, buf10, buf11, 256, XBLOCK=256, num_warps=
4, num_stages=1)
del buf9
del primals_7
return (buf11, primals_2, primals_3, primals_5, primals_6, buf1,
reinterpret_tensor(buf2, (16, 4, 3, 3), (36, 9, 3, 1), 0),
reinterpret_tensor(primals_1, (1, 16, 4, 4), (256, 16, 4, 1), 0),
buf4, buf6, reinterpret_tensor(buf7, (16, 4, 3, 3), (36, 9, 3, 1),
0), reinterpret_tensor(buf8, (1, 16, 4, 4), (256, 16, 4, 1), 0), buf10)
def make_kernel(k):
k = torch.tensor(k, dtype=torch.float32)
if k.ndim == 1:
k = k[None, :] * k[:, None]
k /= k.sum()
return k
def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
pad_x1, pad_y0, pad_y1):
_, channel, in_h, in_w = input.shape
input = input.reshape(-1, in_h, in_w, 1)
_, in_h, in_w, minor = input.shape
kernel_h, kernel_w = kernel.shape
out = input.view(-1, in_h, 1, in_w, 1, minor)
out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
out = out.view(-1, in_h * up_y, in_w * up_x, minor)
out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
max(pad_y1, 0)])
out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
out = out.permute(0, 3, 1, 2)
out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
pad_x0 + pad_x1])
w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
out = F.conv2d(out, w)
out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
out = out.permute(0, 2, 3, 1)
out = out[:, ::down_y, ::down_x, :]
out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
return out.view(-1, channel, out_h, out_w)
def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
], pad[0], pad[1])
return out
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
rest_dim = [1] * (input.ndim - bias.ndim - 1)
if input.ndim == 3:
return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
negative_slope=negative_slope) * scale
else:
return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
negative_slope=negative_slope) * scale
class Blur(nn.Module):
def __init__(self, kernel, pad, upsample_factor=1):
super().__init__()
kernel = make_kernel(kernel)
if upsample_factor > 1:
kernel = kernel * upsample_factor ** 2
self.register_buffer('kernel', kernel)
self.pad = pad
def forward(self, input):
out = upfirdn2d(input, self.kernel, pad=self.pad)
return out
class EqualLinear(nn.Module):
def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
activation=None):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
else:
self.bias = None
self.activation = activation
self.scale = 1 / math.sqrt(in_dim) * lr_mul
self.lr_mul = lr_mul
def forward(self, input):
bias = self.bias * self.lr_mul if self.bias is not None else None
if self.activation:
out = F.linear(input, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(input, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
)
class ModulatedConv2d(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, demodulate=True, upsample=False, downsample=False,
blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.eps = 1e-08
self.kernel_size = kernel_size
self.in_channel = in_channel
self.out_channel = out_channel
self.upsample = upsample
self.downsample = downsample
self.use_style = use_style
if upsample:
factor = 2
p = len(blur_kernel) - factor - (kernel_size - 1)
pad0 = (p + 1) // 2 + factor - 1
pad1 = p // 2 + 1
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
=factor)
if downsample:
factor = 2
p = len(blur_kernel) - factor + (kernel_size - 1)
pad0 = (p + 1) // 2
pad1 = p // 2
self.blur = Blur(blur_kernel, pad=(pad0, pad1))
fan_in = in_channel * kernel_size ** 2
self.scale = 1 / math.sqrt(fan_in)
self.padding = kernel_size // 2
self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
kernel_size, kernel_size))
if use_style:
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
else:
self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
1).fill_(1))
self.demodulate = demodulate
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
)
def forward(self, input, style):
batch, in_channel, height, width = input.shape
if self.use_style:
style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
weight = self.scale * self.weight * style
else:
weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
) * self.modulation
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
weight = weight.view(batch * self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
if self.upsample:
input = input.view(1, batch * in_channel, height, width)
weight = weight.view(batch, self.out_channel, in_channel, self.
kernel_size, self.kernel_size)
weight = weight.transpose(1, 2).reshape(batch * in_channel,
self.out_channel, self.kernel_size, self.kernel_size)
out = F.conv_transpose2d(input, weight, padding=0, stride=2,
groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
out = self.blur(out)
elif self.downsample:
input = self.blur(input)
_, _, height, width = input.shape
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
else:
input = input.view(1, batch * in_channel, height, width)
out = F.conv2d(input, weight, padding=self.padding, groups=batch)
_, _, height, width = out.shape
out = out.view(batch, self.out_channel, height, width)
return out
class FusedLeakyReLU(nn.Module):
def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
super().__init__()
self.bias = nn.Parameter(torch.zeros(channel))
self.negative_slope = negative_slope
self.scale = scale
def forward(self, input):
return fused_leaky_relu(input, self.bias, self.negative_slope, self
.scale)
class StyledConv(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size, style_dim,
use_style=True, upsample=False, downsample=False, blur_kernel=[1, 3,
3, 1], demodulate=True):
super().__init__()
self.use_style = use_style
self.conv = ModulatedConv2d(in_channel, out_channel, kernel_size,
style_dim, use_style=use_style, upsample=upsample, downsample=
downsample, blur_kernel=blur_kernel, demodulate=demodulate)
self.activate = FusedLeakyReLU(out_channel)
def forward(self, input, style=None, noise=None):
out = self.conv(input, style)
out = self.activate(out)
return out
class InResBlockNew(nn.Module):
def __init__(self, in_channel, blur_kernel=[1, 3, 3, 1]):
super().__init__()
self.conv1 = StyledConv(in_channel, in_channel, 3, None,
blur_kernel=blur_kernel, demodulate=True, use_style=False)
self.conv2 = StyledConv(in_channel, in_channel, 3, None,
blur_kernel=blur_kernel, demodulate=True, use_style=False)
def forward(self, input_0):
primals_2 = self.conv1.conv.weight
primals_3 = self.conv1.conv.modulation
primals_4 = self.conv1.activate.bias
primals_5 = self.conv2.conv.weight
primals_6 = self.conv2.conv.modulation
primals_7 = self.conv2.activate.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
ishine/GANsNRoses
|
InResBlock
| false
| 15,638
|
[
"MIT"
] | 969
|
414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
https://github.com/ishine/GANsNRoses/tree/414e9e77c3df47d4ecf7941b5dcfdffec67403ee
|
AttLuong
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AttLuong(torch.nn.Module):
"""
AttLuong: Attention according to Luong that can be used by the
Alignment module.
"""
def __init__(self, q_dim, y_dim, softmax=True):
super().__init__()
self.q_dim = q_dim
self.y_dim = y_dim
self.softmax = softmax
self.W = nn.Linear(self.y_dim, self.q_dim)
def forward(self, query, y):
att = torch.bmm(query, self.W(y).transpose(2, 1))
sim = att.max(2)[0].unsqueeze(1)
if self.softmax:
att = F.softmax(att, dim=2)
return att, sim
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'q_dim': 4, 'y_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_max_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
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 > tmp1
tmp3 = tmp0 == tmp1
tmp4 = tmp0 != tmp0
tmp5 = tmp1 != tmp1
tmp6 = tmp4 > tmp5
tmp7 = tmp2 | tmp6
tmp8 = tmp4 & tmp5
tmp9 = tmp3 | tmp8
tmp10 = tl.full([1], 0, tl.int64)
tmp11 = tl.full([1], 1, tl.int64)
tmp12 = tmp10 < tmp11
tmp13 = tmp9 & tmp12
tmp14 = tmp7 | tmp13
tmp15 = tl.where(tmp14, tmp0, tmp1)
tmp16 = tl.where(tmp14, tmp10, tmp11)
tmp18 = tmp15 > tmp17
tmp19 = tmp15 == tmp17
tmp20 = tmp15 != tmp15
tmp21 = tmp17 != tmp17
tmp22 = tmp20 > tmp21
tmp23 = tmp18 | tmp22
tmp24 = tmp20 & tmp21
tmp25 = tmp19 | tmp24
tmp26 = tl.full([1], 2, tl.int64)
tmp27 = tmp16 < tmp26
tmp28 = tmp25 & tmp27
tmp29 = tmp23 | tmp28
tmp30 = tl.where(tmp29, tmp15, tmp17)
tmp31 = tl.where(tmp29, tmp16, tmp26)
tmp33 = tmp30 > tmp32
tmp34 = tmp30 == tmp32
tmp35 = tmp30 != tmp30
tmp36 = tmp32 != tmp32
tmp37 = tmp35 > tmp36
tmp38 = tmp33 | tmp37
tmp39 = tmp35 & tmp36
tmp40 = tmp34 | tmp39
tmp41 = tl.full([1], 3, tl.int64)
tmp42 = tmp31 < tmp41
tmp43 = tmp40 & tmp42
tmp44 = tmp38 | tmp43
tl.where(tmp44, tmp30, tmp32)
tmp46 = tl.where(tmp44, tmp31, tmp41)
tmp47 = triton_helpers.maximum(tmp0, tmp1)
tmp48 = triton_helpers.maximum(tmp47, tmp17)
tmp49 = triton_helpers.maximum(tmp48, tmp32)
tl.store(out_ptr0 + x0, tmp46, xmask)
tl.store(out_ptr1 + x0, tmp49, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (16,
4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0
), alpha=1, beta=1, out=buf0)
del primals_1
del primals_2
buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(primals_4, reinterpret_tensor(buf0, (4, 4, 4), (
16, 1, 4), 0), out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_max_0[grid(16)](buf1, buf2, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf4 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
del buf0
triton_poi_fused__softmax_1[grid(64)](buf1, buf4, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf5 = buf1
del buf1
triton_poi_fused__softmax_2[grid(64)](buf4, buf5, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf4
return buf5, reinterpret_tensor(buf3, (4, 1, 4), (4, 4, 1), 0
), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0
), buf5, reinterpret_tensor(buf2, (4, 4, 1), (4, 1, 1), 0
), reinterpret_tensor(primals_4, (4, 4, 4), (16, 1, 4), 0)
class AttLuongNew(torch.nn.Module):
"""
AttLuong: Attention according to Luong that can be used by the
Alignment module.
"""
def __init__(self, q_dim, y_dim, softmax=True):
super().__init__()
self.q_dim = q_dim
self.y_dim = y_dim
self.softmax = softmax
self.W = nn.Linear(self.y_dim, self.q_dim)
def forward(self, input_0, input_1):
primals_1 = self.W.weight
primals_2 = self.W.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0], output[1]
|
ishine/NISQA
|
AttLuong
| false
| 15,639
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
FinalLayer
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class LayerNorm(nn.Module):
def __init__(self, features, eps=1e-06):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(features))
self.beta = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.gamma * (x - mean) / (std + self.eps) + self.beta
class normrelu(nn.Module):
def __init__(self):
super(normrelu, self).__init__()
def forward(self, x):
dim = 1
x = F.relu(x) / torch.max(x, dim, keepdim=True)[0]
return x
class LinearBlock(nn.Module):
def __init__(self, pre_dim, dim, activation='none', dropout_rate=0,
use_batch_norm=False, use_layer_norm=False):
self.linear = None
self.bn = None
self.ln = None
self.act = None
self.dropout_layer = None
super(LinearBlock, self).__init__()
if activation == 'relu':
self.act = nn.ReLU()
elif activation == 'tanh':
self.act = nn.Tanh()
elif activation == 'sigmoid':
self.act = nn.Sigmoid()
elif activation == 'normrelu':
self.act = normrelu()
elif activation == 'none':
self.act = None
else:
None
if use_batch_norm:
self.linear = nn.Linear(pre_dim, dim, bias=False)
self.bn = nn.BatchNorm1d(dim, momentum=0.05)
else:
self.linear = nn.Linear(pre_dim, dim)
if use_layer_norm:
self.ln = LayerNorm(dim)
if dropout_rate > 0.0001:
self.dropout_layer = nn.Dropout(p=dropout_rate)
def forward(self, x):
if self.linear is not None:
x = self.linear(x)
if self.bn is not None:
x = self.bn(x)
if self.ln is not None:
x = self.ln(x)
if self.act is not None:
x = self.act(x)
if self.dropout_layer is not None:
x = self.dropout_layer(x)
return x
class FinalLayer(nn.Module):
"""
final classification and bounding box regression layer for RPN KWS
"""
def __init__(self, input_dim, num_class):
super(FinalLayer, self).__init__()
self.linear = LinearBlock(input_dim, input_dim, activation='relu')
self.cls_score_KWS = nn.Linear(input_dim, num_class, bias=True)
self.bbox_score_KWS = nn.Linear(input_dim, 2, bias=True)
def forward(self, x):
x = self.linear(x)
kws_cls_score = self.cls_score_KWS(x)
kws_bbox_pred = self.bbox_score_KWS(x)
return kws_cls_score, kws_bbox_pred
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'num_class': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
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, (2, 4), (4, 1))
assert_size_stride(primals_7, (2,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1,
primals_2, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), (
4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0),
alpha=1, beta=1, out=buf2)
del primals_5
buf3 = empty_strided_cuda((64, 2), (2, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf1, (64, 4), (
4, 1), 0), reinterpret_tensor(primals_6, (4, 2), (1, 4), 0),
alpha=1, beta=1, out=buf3)
del primals_7
return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0
), reinterpret_tensor(buf3, (4, 4, 4, 2), (32, 8, 2, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 4), (4, 1), 0
), primals_6, primals_4, buf4
class LayerNorm(nn.Module):
def __init__(self, features, eps=1e-06):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(features))
self.beta = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.gamma * (x - mean) / (std + self.eps) + self.beta
class normrelu(nn.Module):
def __init__(self):
super(normrelu, self).__init__()
def forward(self, x):
dim = 1
x = F.relu(x) / torch.max(x, dim, keepdim=True)[0]
return x
class LinearBlock(nn.Module):
def __init__(self, pre_dim, dim, activation='none', dropout_rate=0,
use_batch_norm=False, use_layer_norm=False):
self.linear = None
self.bn = None
self.ln = None
self.act = None
self.dropout_layer = None
super(LinearBlock, self).__init__()
if activation == 'relu':
self.act = nn.ReLU()
elif activation == 'tanh':
self.act = nn.Tanh()
elif activation == 'sigmoid':
self.act = nn.Sigmoid()
elif activation == 'normrelu':
self.act = normrelu()
elif activation == 'none':
self.act = None
else:
None
if use_batch_norm:
self.linear = nn.Linear(pre_dim, dim, bias=False)
self.bn = nn.BatchNorm1d(dim, momentum=0.05)
else:
self.linear = nn.Linear(pre_dim, dim)
if use_layer_norm:
self.ln = LayerNorm(dim)
if dropout_rate > 0.0001:
self.dropout_layer = nn.Dropout(p=dropout_rate)
def forward(self, x):
if self.linear is not None:
x = self.linear(x)
if self.bn is not None:
x = self.bn(x)
if self.ln is not None:
x = self.ln(x)
if self.act is not None:
x = self.act(x)
if self.dropout_layer is not None:
x = self.dropout_layer(x)
return x
class FinalLayerNew(nn.Module):
"""
final classification and bounding box regression layer for RPN KWS
"""
def __init__(self, input_dim, num_class):
super(FinalLayerNew, self).__init__()
self.linear = LinearBlock(input_dim, input_dim, activation='relu')
self.cls_score_KWS = nn.Linear(input_dim, num_class, bias=True)
self.bbox_score_KWS = nn.Linear(input_dim, 2, bias=True)
def forward(self, input_0):
primals_1 = self.linear.linear.weight
primals_2 = self.linear.linear.bias
primals_4 = self.cls_score_KWS.weight
primals_5 = self.cls_score_KWS.bias
primals_6 = self.bbox_score_KWS.weight
primals_7 = self.bbox_score_KWS.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0], output[1]
|
ishine/RPN_KWS
|
FinalLayer
| false
| 15,640
|
[
"MIT"
] | 53
|
b54d4010a701a6ec0a9ddf3ab6177a4be6dd6af5
|
https://github.com/ishine/RPN_KWS/tree/b54d4010a701a6ec0a9ddf3ab6177a4be6dd6af5
|
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=256, 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=256, 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]
|
iffiX/machin
|
BasicBlockWN
| false
| 15,641
|
[
"MIT"
] | 287
|
7fa986b1bafdefff117d6ff73d14644a5488de9d
|
https://github.com/iffiX/machin/tree/7fa986b1bafdefff117d6ff73d14644a5488de9d
|
normrelu
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class normrelu(nn.Module):
def __init__(self):
super(normrelu, self).__init__()
def forward(self, x):
dim = 1
x = F.relu(x) / torch.max(x, dim, keepdim=True)[0]
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime 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_div_max_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp3 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = 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')
tmp8 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp9 = triton_helpers.maximum(tmp7, tmp8)
tmp10 = tmp2 / tmp9
tl.store(out_ptr0 + x3, tmp10, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_div_max_relu_0[grid(256)](arg0_1, buf0, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class normreluNew(nn.Module):
def __init__(self):
super(normreluNew, self).__init__()
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ishine/RPN_KWS
|
normrelu
| false
| 15,642
|
[
"MIT"
] | 53
|
b54d4010a701a6ec0a9ddf3ab6177a4be6dd6af5
|
https://github.com/ishine/RPN_KWS/tree/b54d4010a701a6ec0a9ddf3ab6177a4be6dd6af5
|
DiceLoss
|
import torch
import torch.nn as nn
class DiceLoss(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLoss, self).__init__()
self.smooth = smooth
def forward(self, prediction, target):
iflat = prediction.reshape(-1)
tflat = target.reshape(-1)
intersection = (iflat * tflat).sum()
return -(2.0 * intersection + self.smooth) / (iflat.sum() + tflat.
sum() + self.smooth)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_div_mul_neg_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = tmp0 * tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
tmp6 = tl.broadcast_to(tmp0, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = tl.broadcast_to(tmp1, [RBLOCK])
tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0))
tmp12 = 2.0
tmp13 = tmp5 * tmp12
tmp14 = 1.0
tmp15 = tmp13 + tmp14
tmp16 = -tmp15
tmp17 = tmp8 + tmp11
tmp18 = tmp17 + tmp14
tmp19 = tmp16 / tmp18
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp19, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf3 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_div_mul_neg_sum_0[grid(1)](buf3, arg0_1,
arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf3,
class DiceLossNew(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLossNew, self).__init__()
self.smooth = smooth
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
DiceLoss
| false
| 15,643
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
SE_Connect
|
import torch
import torch.nn.functional as F
import torch.nn
import torch.nn as nn
class SE_Connect(nn.Module):
def __init__(self, channels, s=4):
super().__init__()
assert channels % s == 0, '{} % {} != 0'.format(channesl, s)
self.linear1 = nn.Linear(channels, channels // s)
self.linear2 = nn.Linear(channels // s, channels)
def forward(self, x):
out = x.mean(dim=2)
out = F.relu(self.linear1(out))
out = torch.sigmoid(self.linear2(out))
out = x * out.unsqueeze(2)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'channels': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 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
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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_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
x3 = xindex
x0 = xindex % 4
x2 = xindex // 16
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x3, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (1, 4), (4, 1))
assert_size_stride(primals_3, (1,), (1,))
assert_size_stride(primals_4, (4, 1), (1, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mean_0[grid(64)](primals_1, buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf1 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 1), (1, 4), 0), out=buf1)
del primals_2
buf2 = reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 1), 0)
del buf1
buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_1[grid(16)](buf2,
primals_3, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (16, 1), (
1, 0), 0), reinterpret_tensor(primals_4, (1, 4), (1, 1), 0),
alpha=1, beta=1, out=buf3)
del primals_5
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_mul_2[grid(256)](primals_1, buf3, buf4, 256,
XBLOCK=256, num_warps=4, num_stages=1)
return buf4, primals_1, reinterpret_tensor(buf0, (16, 4), (4, 1), 0
), reinterpret_tensor(buf2, (16, 1), (1, 1), 0), buf3, primals_4, buf5
class SE_ConnectNew(nn.Module):
def __init__(self, channels, s=4):
super().__init__()
assert channels % s == 0, '{} % {} != 0'.format(channesl, s)
self.linear1 = nn.Linear(channels, channels // s)
self.linear2 = nn.Linear(channels // s, channels)
def forward(self, input_0):
primals_2 = self.linear1.weight
primals_3 = self.linear1.bias
primals_4 = self.linear2.weight
primals_5 = self.linear2.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ishine/asv-subtools
|
SE_Connect
| false
| 15,644
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
LDEPooling
|
import torch
import torch.nn
class LDEPooling(torch.nn.Module):
"""A novel learnable dictionary encoding layer.
Reference: Weicheng Cai, etc., "A NOVEL LEARNABLE DICTIONARY ENCODING LAYER FOR END-TO-END
LANGUAGE IDENTIFICATION", icassp, 2018
"""
def __init__(self, input_dim, c_num=64, eps=1e-10):
super(LDEPooling, self).__init__()
self.input_dim = input_dim
self.output_dim = input_dim * c_num
self.eps = eps
self.mu = torch.nn.Parameter(torch.randn(input_dim, c_num))
self.s = torch.nn.Parameter(torch.ones(c_num))
self.softmax_for_w = torch.nn.Softmax(dim=3)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
r = inputs.transpose(1, 2).unsqueeze(3) - self.mu
w = self.softmax_for_w(-(self.s ** 2 + self.eps) * torch.sum(r ** 2,
dim=2, keepdim=True))
e = torch.mean(w * r, dim=1)
return e.reshape(-1, self.output_dim, 1)
def get_output_dim(self):
return self.output_dim
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused__softmax_add_mul_neg_pow_sub_sum_0(in_ptr0, in_ptr1,
in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr
):
xnumel = 16
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
x0 = xindex % 4
x1 = xindex // 4
x3 = xindex
tmp0 = tl.load(in_ptr0 + r2, None, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr2 + r2, None, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr2 + (64 + r2), None, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask, eviction_policy=
'evict_last')
tmp15 = tl.load(in_ptr2 + (128 + r2), None, eviction_policy='evict_last')
tmp19 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask, eviction_policy=
'evict_last')
tmp20 = tl.load(in_ptr2 + (192 + r2), None, eviction_policy='evict_last')
tmp1 = tmp0 * tmp0
tmp2 = 1e-10
tmp3 = tmp1 + tmp2
tmp4 = -tmp3
tmp7 = tmp5 - tmp6
tmp8 = tmp7 * tmp7
tmp11 = tmp9 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tmp8 + tmp12
tmp16 = tmp14 - tmp15
tmp17 = tmp16 * tmp16
tmp18 = tmp13 + tmp17
tmp21 = tmp19 - tmp20
tmp22 = tmp21 * tmp21
tmp23 = tmp18 + tmp22
tmp24 = tmp4 * tmp23
tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
tmp27 = tl.where(xmask, tmp25, float('-inf'))
tmp28 = triton_helpers.max2(tmp27, 1)[:, None]
tmp29 = tmp24 - tmp28
tmp30 = tl_math.exp(tmp29)
tmp31 = tl.broadcast_to(tmp30, [XBLOCK, RBLOCK])
tmp33 = tl.where(xmask, tmp31, 0)
tmp34 = tl.sum(tmp33, 1)[:, None]
tl.store(out_ptr0 + (r2 + 64 * x3), tmp24, xmask)
tl.store(out_ptr1 + x3, tmp28, xmask)
tl.store(out_ptr2 + x3, tmp34, xmask)
@triton.jit
def triton_poi_fused__softmax_mean_mul_sub_1(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 64
x2 = xindex // 256
x4 = xindex // 64
x3 = xindex % 256
x5 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 256 * x2), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr2 + 4 * x2, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr3 + 4 * x4, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x3, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr0 + (64 + x0 + 256 * x2), xmask, eviction_policy=
'evict_last')
tmp11 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp14 = tl.load(in_ptr2 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp16 = tl.load(in_ptr3 + (1 + 4 * x4), xmask, eviction_policy='evict_last'
)
tmp20 = tl.load(in_ptr0 + (128 + x0 + 256 * x2), xmask, eviction_policy
='evict_last')
tmp21 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp24 = tl.load(in_ptr2 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp26 = tl.load(in_ptr3 + (2 + 4 * x4), xmask, eviction_policy='evict_last'
)
tmp30 = tl.load(in_ptr0 + (192 + x0 + 256 * x2), xmask, eviction_policy
='evict_last')
tmp31 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp34 = tl.load(in_ptr2 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp36 = tl.load(in_ptr3 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 - tmp1
tmp3 = tl_math.exp(tmp2)
tmp5 = tmp3 / tmp4
tmp8 = tmp6 - tmp7
tmp9 = tmp5 * tmp8
tmp12 = tmp10 - tmp11
tmp13 = tl_math.exp(tmp12)
tmp15 = tmp13 / tmp14
tmp17 = tmp16 - tmp7
tmp18 = tmp15 * tmp17
tmp19 = tmp9 + tmp18
tmp22 = tmp20 - tmp21
tmp23 = tl_math.exp(tmp22)
tmp25 = tmp23 / tmp24
tmp27 = tmp26 - tmp7
tmp28 = tmp25 * tmp27
tmp29 = tmp19 + tmp28
tmp32 = tmp30 - tmp31
tmp33 = tl_math.exp(tmp32)
tmp35 = tmp33 / tmp34
tmp37 = tmp36 - tmp7
tmp38 = tmp35 * tmp37
tmp39 = tmp29 + tmp38
tmp40 = 4.0
tmp41 = tmp39 / tmp40
tl.store(out_ptr0 + x5, tmp41, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 64), (64, 1))
assert_size_stride(primals_3, (64,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 64), (256, 64, 1024, 1), torch.
float32)
buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
get_raw_stream(0)
triton_per_fused__softmax_add_mul_neg_pow_sub_sum_0[grid(16)](primals_3
, primals_1, primals_2, buf0, buf1, buf2, 16, 64, XBLOCK=8,
num_warps=4, num_stages=1)
buf3 = empty_strided_cuda((4, 4, 64), (256, 64, 1), torch.float32)
triton_poi_fused__softmax_mean_mul_sub_1[grid(1024)](buf0, buf1,
buf2, primals_1, primals_2, buf3, 1024, XBLOCK=128, num_warps=4,
num_stages=1)
del buf0
return reinterpret_tensor(buf3, (4, 256, 1), (256, 1, 1), 0
), primals_1, primals_2, primals_3, buf1, buf2
class LDEPoolingNew(torch.nn.Module):
"""A novel learnable dictionary encoding layer.
Reference: Weicheng Cai, etc., "A NOVEL LEARNABLE DICTIONARY ENCODING LAYER FOR END-TO-END
LANGUAGE IDENTIFICATION", icassp, 2018
"""
def __init__(self, input_dim, c_num=64, eps=1e-10):
super(LDEPoolingNew, self).__init__()
self.input_dim = input_dim
self.output_dim = input_dim * c_num
self.eps = eps
self.mu = torch.nn.Parameter(torch.randn(input_dim, c_num))
self.s = torch.nn.Parameter(torch.ones(c_num))
self.softmax_for_w = torch.nn.Softmax(dim=3)
def get_output_dim(self):
return self.output_dim
def forward(self, input_0):
primals_2 = self.mu
primals_3 = self.s
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/asv-subtools
|
LDEPooling
| false
| 15,645
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
TdnnAffine
|
import torch
import torch.nn.functional as F
import torch.nn
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffine(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffine, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.pad:
inputs = F.pad(inputs, (-self.left_context, self.right_context),
mode='constant', value=0)
assert inputs.shape[2] >= self.tot_context
if not self.selected_device and self.mask is not None:
self.mask = to_device(self, self.mask)
self.selected_device = True
filters = (self.weight * self.mask if self.mask is not None else
self.weight)
if self.norm_w:
filters = F.normalize(filters, dim=1)
if self.norm_f:
inputs = F.normalize(inputs, dim=1)
outputs = F.conv1d(inputs, filters, self.bias, stride=self.stride,
padding=0, dilation=1, groups=self.groups)
return outputs
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
@triton.jit
def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 4 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 4), (16, 4, 1))
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_convolution_0[grid(64)](buf1, primals_3, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return buf1, primals_1, primals_2
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffineNew(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffineNew, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
def forward(self, input_0):
primals_2 = self.weight
primals_3 = self.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/asv-subtools
|
TdnnAffine
| false
| 15,646
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
AttCosine
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AttCosine(torch.nn.Module):
"""
AttCosine: Cosine attention that can be used by the Alignment module.
"""
def __init__(self, softmax=True):
super().__init__()
self.softmax = softmax
self.pdist = nn.CosineSimilarity(dim=3)
def forward(self, query, y):
att = self.pdist(query.unsqueeze(2), y.unsqueeze(1))
sim = att.max(2)[0].unsqueeze(1)
if self.softmax:
att = F.softmax(att, dim=2)
return att, sim
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@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 = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x5 = xindex % 16
x6 = xindex // 64
x0 = xindex % 4
x4 = xindex // 256
x8 = xindex % 64
x2 = xindex // 16 % 4
x9 = xindex
tmp0 = tl.load(in_ptr0 + (x5 + 16 * x6), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr0 + (x0 + 16 * x6), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x6), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (8 + x0 + 16 * x6), xmask, eviction_policy=
'evict_last')
tmp9 = tl.load(in_ptr0 + (12 + x0 + 16 * x6), xmask, eviction_policy=
'evict_last')
tmp16 = tl.load(in_ptr1 + (x8 + 64 * x4), xmask, eviction_policy=
'evict_last')
tmp17 = tl.load(in_ptr1 + (x0 + 16 * x2 + 64 * x4), xmask,
eviction_policy='evict_last')
tmp19 = tl.load(in_ptr1 + (4 + x0 + 16 * x2 + 64 * x4), xmask,
eviction_policy='evict_last')
tmp22 = tl.load(in_ptr1 + (8 + x0 + 16 * x2 + 64 * x4), xmask,
eviction_policy='evict_last')
tmp25 = tl.load(in_ptr1 + (12 + x0 + 16 * x2 + 64 * x4), 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 + x9, tmp31, xmask)
@triton.jit
def triton_poi_fused__softmax_max_sum_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 % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (4 + x0 + 64 * x1), xmask)
tmp3 = tl.load(in_ptr0 + (8 + x0 + 64 * x1), xmask)
tmp5 = tl.load(in_ptr0 + (12 + x0 + 64 * x1), xmask)
tmp7 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
tmp8 = tl.load(in_ptr0 + (20 + x0 + 64 * x1), xmask)
tmp10 = tl.load(in_ptr0 + (24 + x0 + 64 * x1), xmask)
tmp12 = tl.load(in_ptr0 + (28 + x0 + 64 * x1), xmask)
tmp15 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
tmp16 = tl.load(in_ptr0 + (36 + x0 + 64 * x1), xmask)
tmp18 = tl.load(in_ptr0 + (40 + x0 + 64 * x1), xmask)
tmp20 = tl.load(in_ptr0 + (44 + x0 + 64 * x1), xmask)
tmp23 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
tmp24 = tl.load(in_ptr0 + (52 + x0 + 64 * x1), xmask)
tmp26 = tl.load(in_ptr0 + (56 + x0 + 64 * x1), xmask)
tmp28 = tl.load(in_ptr0 + (60 + x0 + 64 * x1), xmask)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp9 = tmp7 + tmp8
tmp11 = tmp9 + tmp10
tmp13 = tmp11 + tmp12
tmp14 = triton_helpers.maximum(tmp6, tmp13)
tmp17 = tmp15 + tmp16
tmp19 = tmp17 + tmp18
tmp21 = tmp19 + tmp20
tmp22 = triton_helpers.maximum(tmp14, tmp21)
tmp25 = tmp23 + tmp24
tmp27 = tmp25 + tmp26
tmp29 = tmp27 + tmp28
tmp30 = triton_helpers.maximum(tmp22, tmp29)
tl.store(out_ptr0 + x2, tmp30, xmask)
tl.store(out_ptr1 + x2, tmp30, xmask)
@triton.jit
def triton_poi_fused__softmax_sum_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
x0 = xindex % 4
x3 = xindex // 4
x2 = xindex // 16
x4 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x3), xmask)
tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x3), xmask)
tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x3), xmask)
tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x3), xmask)
tmp7 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x4, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 4
x2 = xindex // 16
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
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, 4), (256, 64, 16, 4, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0[grid(1024)](
arg0_1, arg1_1, buf0, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
buf2 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 64, 1), torch.float32)
triton_poi_fused__softmax_max_sum_1[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__softmax_sum_2[grid(256)](buf0, buf2, buf3, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del buf0
del buf2
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_3[grid(256)](buf3, buf4, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf3
return buf4, reinterpret_tensor(buf1, (4, 1, 4, 4), (16, 16, 4, 1), 0)
class AttCosineNew(torch.nn.Module):
"""
AttCosine: Cosine attention that can be used by the Alignment module.
"""
def __init__(self, softmax=True):
super().__init__()
self.softmax = softmax
self.pdist = nn.CosineSimilarity(dim=3)
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0], output[1]
|
ishine/NISQA
|
AttCosine
| false
| 15,647
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
ChunkSeparationAffine
|
import torch
import torch.nn.functional as F
import torch.nn
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffine(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffine, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.pad:
inputs = F.pad(inputs, (-self.left_context, self.right_context),
mode='constant', value=0)
assert inputs.shape[2] >= self.tot_context
if not self.selected_device and self.mask is not None:
self.mask = to_device(self, self.mask)
self.selected_device = True
filters = (self.weight * self.mask if self.mask is not None else
self.weight)
if self.norm_w:
filters = F.normalize(filters, dim=1)
if self.norm_f:
inputs = F.normalize(inputs, dim=1)
outputs = F.conv1d(inputs, filters, self.bias, stride=self.stride,
padding=0, dilation=1, groups=self.groups)
return outputs
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
class ChunkSeparationAffine(torch.nn.Module):
"""By this component, the chunk will be grouped to two parts, odd and even.
"""
def __init__(self, input_dim, output_dim, **options):
super(ChunkSeparationAffine, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.odd = TdnnAffine(input_dim, output_dim // 2, stride=2, **options)
self.even = TdnnAffine(input_dim, output_dim // 2, stride=2, **options)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if inputs.shape[2] % 2 != 0:
inputs = F.pad(inputs, (0, 1), mode='constant', value=0)
return torch.cat((self.odd(inputs), self.even(inputs[:, :, 1:])), dim=1
)
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn.functional as F
import torch.nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 3
x1 = xindex // 3
x2 = xindex
tmp0 = tl.load(in_ptr0 + (1 + x0 + 4 * x1), xmask)
tl.store(out_ptr0 + x2, tmp0, xmask)
@triton.jit
def triton_poi_fused_cat_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 32
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 2 % 4
x0 = xindex % 2
x2 = xindex // 8
x3 = xindex
tmp0 = x1
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 2, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (x0 + 2 * x1 + 4 * x2), tmp4 & xmask, other=0.0)
tmp6 = tl.load(in_ptr1 + x1, tmp4 & xmask, eviction_policy='evict_last',
other=0.0)
tmp7 = tmp5 + tmp6
tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
tmp9 = tl.where(tmp4, tmp7, tmp8)
tmp10 = tmp0 >= tmp3
tl.full([1], 4, tl.int64)
tmp13 = tl.load(in_ptr2 + (x0 + 2 * (-2 + x1) + 4 * x2), tmp10 & xmask,
other=0.0)
tmp14 = tl.load(in_ptr3 + (-2 + x1), tmp10 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp15 = tmp13 + tmp14
tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype)
tmp17 = tl.where(tmp10, tmp15, tmp16)
tmp18 = tl.where(tmp4, tmp9, tmp17)
tl.store(out_ptr0 + x3, tmp18, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (2, 4, 1), (4, 1, 1))
assert_size_stride(primals_3, (2,), (1,))
assert_size_stride(primals_4, (2, 4, 1), (4, 1, 1))
assert_size_stride(primals_5, (2,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(2,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf0, (4, 2, 2), (4, 2, 1))
buf1 = empty_strided_cuda((4, 4, 3), (12, 3, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_constant_pad_nd_0[grid(48)](primals_1, buf1, 48,
XBLOCK=64, num_warps=1, num_stages=1)
buf2 = extern_kernels.convolution(buf1, primals_4, stride=(2,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf2, (4, 2, 2), (4, 2, 1))
buf3 = empty_strided_cuda((4, 4, 2), (8, 2, 1), torch.float32)
triton_poi_fused_cat_1[grid(32)](buf0, primals_3, buf2, primals_5,
buf3, 32, XBLOCK=32, num_warps=1, num_stages=1)
del buf0
del buf2
del primals_3
del primals_5
return buf3, primals_1, primals_2, primals_4, buf1
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffine(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffine, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.pad:
inputs = F.pad(inputs, (-self.left_context, self.right_context),
mode='constant', value=0)
assert inputs.shape[2] >= self.tot_context
if not self.selected_device and self.mask is not None:
self.mask = to_device(self, self.mask)
self.selected_device = True
filters = (self.weight * self.mask if self.mask is not None else
self.weight)
if self.norm_w:
filters = F.normalize(filters, dim=1)
if self.norm_f:
inputs = F.normalize(inputs, dim=1)
outputs = F.conv1d(inputs, filters, self.bias, stride=self.stride,
padding=0, dilation=1, groups=self.groups)
return outputs
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
class ChunkSeparationAffineNew(torch.nn.Module):
"""By this component, the chunk will be grouped to two parts, odd and even.
"""
def __init__(self, input_dim, output_dim, **options):
super(ChunkSeparationAffineNew, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.odd = TdnnAffine(input_dim, output_dim // 2, stride=2, **options)
self.even = TdnnAffine(input_dim, output_dim // 2, stride=2, **options)
def forward(self, input_0):
primals_2 = self.odd.weight
primals_3 = self.odd.bias
primals_4 = self.even.weight
primals_5 = self.even.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ishine/asv-subtools
|
ChunkSeparationAffine
| false
| 15,648
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
FocalLoss
|
import torch
import torch.nn as nn
class FocalLoss(nn.Module):
"""FocalLoss.
.. seealso::
Lin, Tsung-Yi, et al. "Focal loss for dense object detection."
Proceedings of the IEEE international conference on computer vision. 2017.
Args:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
Attributes:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
"""
def __init__(self, gamma=2, alpha=0.25, eps=1e-07):
super(FocalLoss, self).__init__()
self.gamma = gamma
self.alpha = alpha
self.eps = eps
def forward(self, input, target):
input = input.clamp(self.eps, 1.0 - self.eps)
cross_entropy = -(target * torch.log(input) + (1 - target) * torch.
log(1 - input))
logpt = -cross_entropy
pt = torch.exp(logpt)
at = self.alpha * target + (1 - self.alpha) * (1 - target)
balanced_cross_entropy = -at * logpt
focal_loss = balanced_cross_entropy * (1 - pt) ** self.gamma
return focal_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 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_exp_log_mul_neg_pow_rsub_sum_0(in_ptr0,
in_ptr1, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp9 = tl.load(in_ptr1 + r0, None)
tmp1 = 0.25
tmp2 = tmp0 * tmp1
tmp3 = 1.0
tmp4 = tmp3 - tmp0
tmp5 = 0.75
tmp6 = tmp4 * tmp5
tmp7 = tmp2 + tmp6
tmp8 = -tmp7
tmp10 = 1e-07
tmp11 = triton_helpers.maximum(tmp9, tmp10)
tmp12 = 0.9999999
tmp13 = triton_helpers.minimum(tmp11, tmp12)
tmp14 = tl_math.log(tmp13)
tmp15 = tmp0 * tmp14
tmp16 = tmp3 - tmp13
tmp17 = tl_math.log(tmp16)
tmp18 = tmp4 * tmp17
tmp19 = tmp15 + tmp18
tmp20 = -tmp19
tmp21 = -tmp20
tmp22 = tmp8 * tmp21
tmp23 = tl_math.exp(tmp21)
tmp24 = tmp3 - tmp23
tmp25 = tmp24 * tmp24
tmp26 = tmp22 * tmp25
tmp27 = tl.broadcast_to(tmp26, [RBLOCK])
tmp29 = triton_helpers.promote_to_tensor(tl.sum(tmp27, 0))
tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp29, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
get_raw_stream(0)
triton_per_fused_add_clamp_exp_log_mul_neg_pow_rsub_sum_0[grid(1)](
arg1_1, arg0_1, buf0, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf0,
class FocalLossNew(nn.Module):
"""FocalLoss.
.. seealso::
Lin, Tsung-Yi, et al. "Focal loss for dense object detection."
Proceedings of the IEEE international conference on computer vision. 2017.
Args:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
Attributes:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
"""
def __init__(self, gamma=2, alpha=0.25, eps=1e-07):
super(FocalLossNew, self).__init__()
self.gamma = gamma
self.alpha = alpha
self.eps = eps
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
FocalLoss
| false
| 15,649
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
L2loss
|
import torch
import torch.nn as nn
class L2loss(nn.Module):
"""
Euclidean loss also known as L2 loss. Compute the sum of the squared difference between the two images.
"""
def __init__(self):
super(L2loss, self).__init__()
def forward(self, input, target):
return torch.sum((input - target) ** 2) / 2
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime 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_div_pow_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = tmp0 - tmp1
tmp3 = tmp2 * tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tmp7 = 0.5
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((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_div_pow_sub_sum_0[grid(1)](buf1, arg0_1, arg1_1, 1,
256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf1,
class L2lossNew(nn.Module):
"""
Euclidean loss also known as L2 loss. Compute the sum of the squared difference between the two images.
"""
def __init__(self):
super(L2lossNew, 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]
|
ivadomed-profile-analysis-project/ivadomed
|
L2loss
| false
| 15,650
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
SoftmaxAffineLayer
|
import torch
import torch.nn.functional as F
import torch.nn
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffine(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffine, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.pad:
inputs = F.pad(inputs, (-self.left_context, self.right_context),
mode='constant', value=0)
assert inputs.shape[2] >= self.tot_context
if not self.selected_device and self.mask is not None:
self.mask = to_device(self, self.mask)
self.selected_device = True
filters = (self.weight * self.mask if self.mask is not None else
self.weight)
if self.norm_w:
filters = F.normalize(filters, dim=1)
if self.norm_f:
inputs = F.normalize(inputs, dim=1)
outputs = F.conv1d(inputs, filters, self.bias, stride=self.stride,
padding=0, dilation=1, groups=self.groups)
return outputs
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
class SoftmaxAffineLayer(torch.nn.Module):
""" An usual 2-fold softmax layer with an affine transform.
@dim: which dim to apply softmax on
"""
def __init__(self, input_dim, output_dim, context=[0], dim=1, log=True,
bias=True, groups=1, t=1.0, special_init=False):
super(SoftmaxAffineLayer, self).__init__()
self.affine = TdnnAffine(input_dim, output_dim, context=context,
bias=bias, groups=groups)
self.t = t
if log:
self.softmax = torch.nn.LogSoftmax(dim=dim)
else:
self.softmax = torch.nn.Softmax(dim=dim)
if special_init:
torch.nn.init.xavier_uniform_(self.affine.weight, gain=torch.nn
.init.calculate_gain('sigmoid'))
def forward(self, inputs):
"""
@inputs: any, such as a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
return self.softmax(self.affine(inputs) / self.t)
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn.functional as F
import torch.nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused__log_softmax_convolution_0(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
tmp1 = tl.load(in_ptr1 + 0)
tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
tmp6 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
tmp7 = tl.load(in_ptr1 + 1)
tmp8 = tl.broadcast_to(tmp7, [XBLOCK])
tmp12 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
tmp13 = tl.load(in_ptr1 + 2)
tmp14 = tl.broadcast_to(tmp13, [XBLOCK])
tmp18 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
tmp19 = tl.load(in_ptr1 + 3)
tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
tmp3 = tmp0 + tmp2
tmp4 = 1.0
tmp5 = tmp3 * tmp4
tmp9 = tmp6 + tmp8
tmp10 = tmp9 * tmp4
tmp11 = triton_helpers.maximum(tmp5, tmp10)
tmp15 = tmp12 + tmp14
tmp16 = tmp15 * tmp4
tmp17 = triton_helpers.maximum(tmp11, tmp16)
tmp21 = tmp18 + tmp20
tmp22 = tmp21 * tmp4
tmp23 = triton_helpers.maximum(tmp17, tmp22)
tmp24 = tmp5 - tmp23
tmp25 = tmp24 * tmp4
tmp26 = tl_math.exp(tmp25)
tmp27 = tmp10 - tmp23
tmp28 = tmp27 * tmp4
tmp29 = tl_math.exp(tmp28)
tmp30 = tmp26 + tmp29
tmp31 = tmp16 - tmp23
tmp32 = tmp31 * tmp4
tmp33 = tl_math.exp(tmp32)
tmp34 = tmp30 + tmp33
tmp35 = tmp22 - tmp23
tmp36 = tmp35 * tmp4
tmp37 = tl_math.exp(tmp36)
tmp38 = tmp34 + tmp37
tl.store(out_ptr0 + x2, tmp23, xmask)
tl.store(out_ptr1 + x2, tmp38, xmask)
@triton.jit
def triton_poi_fused__log_softmax_convolution_1(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 4 % 4
x0 = xindex % 4
x2 = xindex // 16
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp8 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp3 = 1.0
tmp4 = tmp2 * tmp3
tmp6 = tmp4 - tmp5
tmp7 = tmp6 * tmp3
tmp9 = tl_math.log(tmp8)
tmp10 = tmp7 - tmp9
tl.store(in_out_ptr0 + x3, tmp10, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 4), (16, 4, 1))
buf1 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
buf2 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__log_softmax_convolution_0[grid(16)](buf0,
primals_3, buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf3 = buf0
del buf0
triton_poi_fused__log_softmax_convolution_1[grid(64)](buf3,
primals_3, buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
del buf1
del buf2
del primals_3
return buf3, primals_1, primals_2, buf3
def to_device(device_object, tensor):
"""
Select device for non-parameters tensor w.r.t model or tensor which has been specified a device.
"""
if isinstance(device_object, torch.nn.Module):
next(device_object.parameters()).device
elif isinstance(device_object, torch.Tensor):
pass
return tensor
class TdnnAffine(torch.nn.Module):
""" An implemented tdnn affine component by conv1d
y = splice(w * x, context) + b
@input_dim: number of dims of frame <=> inputs channels of conv
@output_dim: number of layer nodes <=> outputs channels of conv
@context: a list of context
e.g. [-2,0,2]
If context is [0], then the TdnnAffine is equal to linear layer.
"""
def __init__(self, input_dim, output_dim, context=[0], bias=True, pad=
True, stride=1, groups=1, norm_w=False, norm_f=False):
super(TdnnAffine, self).__init__()
assert input_dim % groups == 0
for index in range(0, len(context) - 1):
if context[index] >= context[index + 1]:
raise ValueError(
'Context tuple {} is invalid, such as the order.'.
format(context))
self.input_dim = input_dim
self.output_dim = output_dim
self.context = context
self.bool_bias = bias
self.pad = pad
self.groups = groups
self.norm_w = norm_w
self.norm_f = norm_f
self.stride = stride
self.left_context = context[0] if context[0] < 0 else 0
self.right_context = context[-1] if context[-1] > 0 else 0
self.tot_context = self.right_context - self.left_context + 1
if self.tot_context > 1 and self.norm_f:
self.norm_f = False
None
kernel_size = self.tot_context,
self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim //
groups, *kernel_size))
if self.bool_bias:
self.bias = torch.nn.Parameter(torch.randn(output_dim))
else:
self.register_parameter('bias', None)
self.init_weight()
if len(context) != self.tot_context:
self.mask = torch.tensor([[[(1 if index in context else 0) for
index in range(self.left_context, self.right_context + 1)]]])
else:
self.mask = None
self.selected_device = False
def init_weight(self):
torch.nn.init.normal_(self.weight, 0.0, 0.01)
if self.bias is not None:
torch.nn.init.constant_(self.bias, 0.0)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.pad:
inputs = F.pad(inputs, (-self.left_context, self.right_context),
mode='constant', value=0)
assert inputs.shape[2] >= self.tot_context
if not self.selected_device and self.mask is not None:
self.mask = to_device(self, self.mask)
self.selected_device = True
filters = (self.weight * self.mask if self.mask is not None else
self.weight)
if self.norm_w:
filters = F.normalize(filters, dim=1)
if self.norm_f:
inputs = F.normalize(inputs, dim=1)
outputs = F.conv1d(inputs, filters, self.bias, stride=self.stride,
padding=0, dilation=1, groups=self.groups)
return outputs
def extra_repr(self):
return (
'{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, groups={groups}, norm_w={norm_w}, norm_f={norm_f}'
.format(**self.__dict__))
@classmethod
def thop_count(self, m, x, y):
x = x[0]
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()
bias_ops = 1 if m.bias is not None else 0
total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
m.total_ops += torch.DoubleTensor([int(total_ops)])
class SoftmaxAffineLayerNew(torch.nn.Module):
""" An usual 2-fold softmax layer with an affine transform.
@dim: which dim to apply softmax on
"""
def __init__(self, input_dim, output_dim, context=[0], dim=1, log=True,
bias=True, groups=1, t=1.0, special_init=False):
super(SoftmaxAffineLayerNew, self).__init__()
self.affine = TdnnAffine(input_dim, output_dim, context=context,
bias=bias, groups=groups)
self.t = t
if log:
self.softmax = torch.nn.LogSoftmax(dim=dim)
else:
self.softmax = torch.nn.Softmax(dim=dim)
if special_init:
torch.nn.init.xavier_uniform_(self.affine.weight, gain=torch.nn
.init.calculate_gain('sigmoid'))
def forward(self, input_0):
primals_2 = self.affine.weight
primals_3 = self.affine.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/asv-subtools
|
SoftmaxAffineLayer
| false
| 15,651
|
[
"Apache-2.0"
] | 370
|
597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
https://github.com/ishine/asv-subtools/tree/597dcb29a772b8113dbe7ab64f0d4cc1da298707
|
TverskyLoss
|
import torch
import torch.nn as nn
class TverskyLoss(nn.Module):
"""Tversky Loss.
.. seealso::
Salehi, Seyed Sadegh Mohseni, Deniz Erdogmus, and Ali Gholipour. "Tversky loss function for image segmentation
using 3D fully convolutional deep networks." International Workshop on Machine Learning in Medical Imaging.
Springer, Cham, 2017.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Notes:
- setting alpha=beta=0.5: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1706.05721.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, smooth=1.0):
super(TverskyLoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.smooth = smooth
def tversky_index(self, y_pred, y_true):
"""Compute Tversky index.
Args:
y_pred (torch Tensor): Prediction.
y_true (torch Tensor): Target.
Returns:
float: Tversky index.
"""
y_true = y_true.float()
tp = torch.sum(y_true * y_pred)
fn = torch.sum(y_true * (1 - y_pred))
fp = torch.sum((1 - y_true) * y_pred)
numerator = tp + self.smooth
denominator = tp + self.alpha * fp + self.beta * fn + self.smooth
tversky_label = numerator / denominator
return tversky_label
def forward(self, input, target):
n_classes = input.shape[1]
tversky_sum = 0.0
for i_label in range(n_classes):
y_pred, y_true = input[:, i_label], target[:, i_label]
tversky_sum += self.tversky_index(y_pred, y_true)
return -tversky_sum / n_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_add_div_mul_neg_rsub_sum_0(in_out_ptr1, in_ptr0,
in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex % 16
r1 = rindex // 16
tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
tmp17 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
tmp18 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
tmp33 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
tmp34 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
tmp49 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
tmp50 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
tmp2 = tmp0 * tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp5 = tl.sum(tmp3, 1)[:, None]
tmp6 = 1.0
tmp7 = tmp6 - tmp0
tmp8 = tmp7 * tmp1
tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
tmp11 = tl.sum(tmp9, 1)[:, None]
tmp12 = tmp6 - tmp1
tmp13 = tmp0 * tmp12
tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
tmp16 = tl.sum(tmp14, 1)[:, None]
tmp19 = tmp17 * tmp18
tmp20 = tl.broadcast_to(tmp19, [XBLOCK, RBLOCK])
tmp22 = tl.sum(tmp20, 1)[:, None]
tmp23 = tmp6 - tmp17
tmp24 = tmp23 * tmp18
tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
tmp27 = tl.sum(tmp25, 1)[:, None]
tmp28 = tmp6 - tmp18
tmp29 = tmp17 * tmp28
tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
tmp32 = tl.sum(tmp30, 1)[:, None]
tmp35 = tmp33 * tmp34
tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
tmp38 = tl.sum(tmp36, 1)[:, None]
tmp39 = tmp6 - tmp33
tmp40 = tmp39 * tmp34
tmp41 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
tmp43 = tl.sum(tmp41, 1)[:, None]
tmp44 = tmp6 - tmp34
tmp45 = tmp33 * tmp44
tmp46 = tl.broadcast_to(tmp45, [XBLOCK, RBLOCK])
tmp48 = tl.sum(tmp46, 1)[:, None]
tmp51 = tmp49 * tmp50
tmp52 = tl.broadcast_to(tmp51, [XBLOCK, RBLOCK])
tmp54 = tl.sum(tmp52, 1)[:, None]
tmp55 = tmp6 - tmp49
tmp56 = tmp55 * tmp50
tmp57 = tl.broadcast_to(tmp56, [XBLOCK, RBLOCK])
tmp59 = tl.sum(tmp57, 1)[:, None]
tmp60 = tmp6 - tmp50
tmp61 = tmp49 * tmp60
tmp62 = tl.broadcast_to(tmp61, [XBLOCK, RBLOCK])
tmp64 = tl.sum(tmp62, 1)[:, None]
tmp65 = tmp5 + tmp6
tmp66 = 0.7
tmp67 = tmp11 * tmp66
tmp68 = tmp5 + tmp67
tmp69 = 0.3
tmp70 = tmp16 * tmp69
tmp71 = tmp68 + tmp70
tmp72 = tmp71 + tmp6
tmp73 = tmp65 / tmp72
tmp74 = 0.0
tmp75 = tmp73 + tmp74
tmp76 = tmp22 + tmp6
tmp77 = tmp27 * tmp66
tmp78 = tmp22 + tmp77
tmp79 = tmp32 * tmp69
tmp80 = tmp78 + tmp79
tmp81 = tmp80 + tmp6
tmp82 = tmp76 / tmp81
tmp83 = tmp75 + tmp82
tmp84 = tmp54 + tmp6
tmp85 = tmp59 * tmp66
tmp86 = tmp54 + tmp85
tmp87 = tmp64 * tmp69
tmp88 = tmp86 + tmp87
tmp89 = tmp88 + tmp6
tmp90 = tmp84 / tmp89
tmp91 = tmp83 + tmp90
tmp92 = tmp38 + tmp6
tmp93 = tmp43 * tmp66
tmp94 = tmp38 + tmp93
tmp95 = tmp48 * tmp69
tmp96 = tmp94 + tmp95
tmp97 = tmp96 + tmp6
tmp98 = tmp92 / tmp97
tmp99 = tmp91 + tmp98
tmp100 = -tmp99
tmp101 = 0.25
tmp102 = tmp100 * tmp101
tl.debug_barrier()
tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp102, 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)
buf10 = empty_strided_cuda((), (), torch.float32)
buf13 = buf10
del buf10
get_raw_stream(0)
triton_per_fused_add_div_mul_neg_rsub_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,
class TverskyLossNew(nn.Module):
"""Tversky Loss.
.. seealso::
Salehi, Seyed Sadegh Mohseni, Deniz Erdogmus, and Ali Gholipour. "Tversky loss function for image segmentation
using 3D fully convolutional deep networks." International Workshop on Machine Learning in Medical Imaging.
Springer, Cham, 2017.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Notes:
- setting alpha=beta=0.5: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1706.05721.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, smooth=1.0):
super(TverskyLossNew, self).__init__()
self.alpha = alpha
self.beta = beta
self.smooth = smooth
def tversky_index(self, y_pred, y_true):
"""Compute Tversky index.
Args:
y_pred (torch Tensor): Prediction.
y_true (torch Tensor): Target.
Returns:
float: Tversky index.
"""
y_true = y_true.float()
tp = torch.sum(y_true * y_pred)
fn = torch.sum(y_true * (1 - y_pred))
fp = torch.sum((1 - y_true) * y_pred)
numerator = tp + self.smooth
denominator = tp + self.alpha * fp + self.beta * fn + self.smooth
tversky_label = numerator / denominator
return tversky_label
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
TverskyLoss
| false
| 15,652
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
MultiClassDiceLoss
|
import torch
import torch.nn as nn
class DiceLoss(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLoss, self).__init__()
self.smooth = smooth
def forward(self, prediction, target):
iflat = prediction.reshape(-1)
tflat = target.reshape(-1)
intersection = (iflat * tflat).sum()
return -(2.0 * intersection + self.smooth) / (iflat.sum() + tflat.
sum() + self.smooth)
class MultiClassDiceLoss(nn.Module):
"""Multi-class Dice Loss.
Inspired from https://arxiv.org/pdf/1802.10508.
Args:
classes_of_interest (list): List containing the index of a class which its dice will be added to the loss.
If is None all classes are considered.
Attributes:
classes_of_interest (list): List containing the index of a class which its dice will be added to the loss.
If is None all classes are considered.
dice_loss (DiceLoss): Class computing the Dice loss.
"""
def __init__(self, classes_of_interest=None):
super(MultiClassDiceLoss, self).__init__()
self.classes_of_interest = classes_of_interest
self.dice_loss = DiceLoss()
def forward(self, prediction, target):
dice_per_class = 0
n_classes = prediction.shape[1]
if self.classes_of_interest is None:
self.classes_of_interest = range(n_classes)
for i in self.classes_of_interest:
dice_per_class += self.dice_loss(prediction[:, i], target[:, i])
return dice_per_class / len(self.classes_of_interest)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_div_mul_neg_sum_0(in_out_ptr1, 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 + (64 * (r0 // 16) + r0 % 16), None)
tmp1 = tl.load(in_ptr1 + (64 * (r0 // 16) + r0 % 16), None)
tmp12 = tl.load(in_ptr0 + (16 + 64 * (r0 // 16) + r0 % 16), None)
tmp13 = tl.load(in_ptr1 + (16 + 64 * (r0 // 16) + r0 % 16), None)
tmp24 = tl.load(in_ptr0 + (48 + 64 * (r0 // 16) + r0 % 16), None)
tmp25 = tl.load(in_ptr1 + (48 + 64 * (r0 // 16) + r0 % 16), None)
tmp36 = tl.load(in_ptr0 + (32 + 64 * (r0 // 16) + r0 % 16), None)
tmp37 = tl.load(in_ptr1 + (32 + 64 * (r0 // 16) + r0 % 16), None)
tmp2 = tmp0 * tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp5 = tl.sum(tmp3, 1)[:, None]
tmp6 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp8 = tl.sum(tmp6, 1)[:, None]
tmp9 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
tmp11 = tl.sum(tmp9, 1)[:, None]
tmp14 = tmp12 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.sum(tmp15, 1)[:, None]
tmp18 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
tmp20 = tl.sum(tmp18, 1)[:, None]
tmp21 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
tmp23 = tl.sum(tmp21, 1)[:, None]
tmp26 = tmp24 * tmp25
tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
tmp29 = tl.sum(tmp27, 1)[:, None]
tmp30 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
tmp32 = tl.sum(tmp30, 1)[:, None]
tmp33 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK])
tmp35 = tl.sum(tmp33, 1)[:, None]
tmp38 = tmp36 * tmp37
tmp39 = tl.broadcast_to(tmp38, [XBLOCK, RBLOCK])
tmp41 = tl.sum(tmp39, 1)[:, None]
tmp42 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
tmp44 = tl.sum(tmp42, 1)[:, None]
tmp45 = tl.broadcast_to(tmp37, [XBLOCK, RBLOCK])
tmp47 = tl.sum(tmp45, 1)[:, None]
tmp48 = 2.0
tmp49 = tmp5 * tmp48
tmp50 = 1.0
tmp51 = tmp49 + tmp50
tmp52 = -tmp51
tmp53 = tmp8 + tmp11
tmp54 = tmp53 + tmp50
tmp55 = tmp52 / tmp54
tmp56 = 0.0
tmp57 = tmp55 + tmp56
tmp58 = tmp17 * tmp48
tmp59 = tmp58 + tmp50
tmp60 = -tmp59
tmp61 = tmp20 + tmp23
tmp62 = tmp61 + tmp50
tmp63 = tmp60 / tmp62
tmp64 = tmp57 + tmp63
tmp65 = tmp41 * tmp48
tmp66 = tmp65 + tmp50
tmp67 = -tmp66
tmp68 = tmp44 + tmp47
tmp69 = tmp68 + tmp50
tmp70 = tmp67 / tmp69
tmp71 = tmp64 + tmp70
tmp72 = tmp29 * tmp48
tmp73 = tmp72 + tmp50
tmp74 = -tmp73
tmp75 = tmp32 + tmp35
tmp76 = tmp75 + tmp50
tmp77 = tmp74 / tmp76
tmp78 = tmp71 + tmp77
tmp79 = 0.25
tmp80 = tmp78 * tmp79
tl.debug_barrier()
tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp80, 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)
buf10 = empty_strided_cuda((), (), torch.float32)
buf13 = buf10
del buf10
get_raw_stream(0)
triton_per_fused_add_div_mul_neg_sum_0[grid(1)](buf13, arg0_1,
arg1_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf13,
class DiceLoss(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLoss, self).__init__()
self.smooth = smooth
def forward(self, prediction, target):
iflat = prediction.reshape(-1)
tflat = target.reshape(-1)
intersection = (iflat * tflat).sum()
return -(2.0 * intersection + self.smooth) / (iflat.sum() + tflat.
sum() + self.smooth)
class MultiClassDiceLossNew(nn.Module):
"""Multi-class Dice Loss.
Inspired from https://arxiv.org/pdf/1802.10508.
Args:
classes_of_interest (list): List containing the index of a class which its dice will be added to the loss.
If is None all classes are considered.
Attributes:
classes_of_interest (list): List containing the index of a class which its dice will be added to the loss.
If is None all classes are considered.
dice_loss (DiceLoss): Class computing the Dice loss.
"""
def __init__(self, classes_of_interest=None):
super(MultiClassDiceLossNew, self).__init__()
self.classes_of_interest = classes_of_interest
self.dice_loss = DiceLoss()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
MultiClassDiceLoss
| false
| 15,653
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
LinearGLUBlock
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class LinearGLUBlock(nn.Module):
"""A linear GLU block.
Args:
idim (int): input and output dimension
"""
def __init__(self, idim):
super().__init__()
self.fc = nn.Linear(idim, idim * 2)
def forward(self, xs):
return F.glu(self.fc(xs), dim=-1)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'idim': 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_glu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x2, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (8, 4), (4, 1))
assert_size_stride(primals_2, (8,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 8), (8, 1), torch.float32)
extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64,
4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 8), (1, 4), 0
), alpha=1, beta=1, out=buf0)
del primals_1
del primals_2
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_glu_0[grid(256)](buf0, buf1, 256, XBLOCK=256,
num_warps=4, num_stages=1)
return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf0, (4, 4, 4, 8), (128, 32, 8, 1), 0)
class LinearGLUBlockNew(nn.Module):
"""A linear GLU block.
Args:
idim (int): input and output dimension
"""
def __init__(self, idim):
super().__init__()
self.fc = nn.Linear(idim, idim * 2)
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]
|
ishine/neural_sp
|
LinearGLUBlock
| false
| 15,654
|
[
"Apache-2.0"
] | 577
|
7995613541d994976b00d80dcc12e2835163acfb
|
https://github.com/ishine/neural_sp/tree/7995613541d994976b00d80dcc12e2835163acfb
|
LayerNorm2D
|
import torch
import torch.nn as nn
class LayerNorm2D(nn.Module):
"""Layer normalization for CNN outputs."""
def __init__(self, channel, idim, eps=1e-12):
super(LayerNorm2D, self).__init__()
self.norm = nn.LayerNorm([channel, idim], eps=eps)
def forward(self, xs):
"""Forward pass.
Args:
xs (FloatTensor): `[B, C, T, F]`
Returns:
xs (FloatTensor): `[B, C, T, F]`
"""
_B, _C, _T, _F = xs.size()
xs = xs.transpose(2, 1).contiguous()
xs = self.norm(xs)
xs = xs.transpose(2, 1)
return xs
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'channel': 4, 'idim': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_clone_native_layer_norm_0(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr
):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex % 4
r3 = rindex // 4
x0 = xindex % 4
x1 = xindex // 4
x4 = xindex
r5 = rindex
tmp0 = tl.load(in_ptr0 + (r2 + 4 * x0 + 16 * r3 + 64 * x1), xmask,
other=0.0)
tmp24 = tl.load(in_ptr1 + r5, None, eviction_policy='evict_last')
tmp26 = tl.load(in_ptr2 + r5, None, eviction_policy='evict_last')
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tl.where(xmask, tmp1, 0)
tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
tmp6 = tl.where(xmask, tmp4, 0)
tmp7 = tl.sum(tmp6, 1)[:, None]
tmp8 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
tmp15 = tl.where(xmask, tmp13, 0)
tmp16 = tl.sum(tmp15, 1)[:, None]
tmp17 = 16.0
tmp18 = tmp16 / tmp17
tmp19 = 1e-12
tmp20 = tmp18 + tmp19
tmp21 = libdevice.rsqrt(tmp20)
tmp22 = tmp0 - tmp10
tmp23 = tmp22 * tmp21
tmp25 = tmp23 * tmp24
tmp27 = tmp25 + tmp26
tl.debug_barrier()
tl.store(in_out_ptr0 + x4, tmp21, xmask)
tl.store(out_ptr1 + (r5 + 16 * x4), tmp27, xmask)
tl.store(out_ptr0 + x4, tmp10, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf3 = reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0)
del buf1
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_per_fused_clone_native_layer_norm_0[grid(16)](buf3,
primals_1, primals_2, primals_3, buf0, buf4, 16, 16, XBLOCK=8,
num_warps=2, num_stages=1)
del primals_2
del primals_3
return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 4, 16, 1), 0
), primals_1, buf0, buf3
class LayerNorm2DNew(nn.Module):
"""Layer normalization for CNN outputs."""
def __init__(self, channel, idim, eps=1e-12):
super(LayerNorm2DNew, self).__init__()
self.norm = nn.LayerNorm([channel, idim], eps=eps)
def forward(self, input_0):
primals_2 = self.norm.weight
primals_3 = self.norm.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ishine/neural_sp
|
LayerNorm2D
| false
| 15,655
|
[
"Apache-2.0"
] | 577
|
7995613541d994976b00d80dcc12e2835163acfb
|
https://github.com/ishine/neural_sp/tree/7995613541d994976b00d80dcc12e2835163acfb
|
FocalDiceLoss
|
import torch
import torch.nn as nn
class DiceLoss(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLoss, self).__init__()
self.smooth = smooth
def forward(self, prediction, target):
iflat = prediction.reshape(-1)
tflat = target.reshape(-1)
intersection = (iflat * tflat).sum()
return -(2.0 * intersection + self.smooth) / (iflat.sum() + tflat.
sum() + self.smooth)
class FocalLoss(nn.Module):
"""FocalLoss.
.. seealso::
Lin, Tsung-Yi, et al. "Focal loss for dense object detection."
Proceedings of the IEEE international conference on computer vision. 2017.
Args:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
Attributes:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
"""
def __init__(self, gamma=2, alpha=0.25, eps=1e-07):
super(FocalLoss, self).__init__()
self.gamma = gamma
self.alpha = alpha
self.eps = eps
def forward(self, input, target):
input = input.clamp(self.eps, 1.0 - self.eps)
cross_entropy = -(target * torch.log(input) + (1 - target) * torch.
log(1 - input))
logpt = -cross_entropy
pt = torch.exp(logpt)
at = self.alpha * target + (1 - self.alpha) * (1 - target)
balanced_cross_entropy = -at * logpt
focal_loss = balanced_cross_entropy * (1 - pt) ** self.gamma
return focal_loss.sum()
class FocalDiceLoss(nn.Module):
"""FocalDiceLoss.
.. seealso::
Wong, Ken CL, et al. "3D segmentation with exponential logarithmic loss for highly unbalanced object sizes."
International Conference on Medical Image Computing and Computer-Assisted Intervention. Springer, Cham, 2018.
Args:
beta (float): Value from 0 to 1, indicating the weight of the dice loss.
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
Attributes:
beta (float): Value from 0 to 1, indicating the weight of the dice loss.
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
"""
def __init__(self, beta=1, gamma=2, alpha=0.25):
super().__init__()
self.beta = beta
self.focal = FocalLoss(gamma, alpha)
self.dice = DiceLoss()
def forward(self, input, target):
dc_loss = -self.dice(input, target)
fc_loss = self.focal(input, target)
loss = torch.log(torch.clamp(fc_loss, 1e-07)) - self.beta * torch.log(
torch.clamp(dc_loss, 1e-07))
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
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_div_exp_log_mul_neg_pow_rsub_sub_sum_0(
in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp9 = tl.load(in_ptr1 + r0, None)
tmp1 = 0.25
tmp2 = tmp0 * tmp1
tmp3 = 1.0
tmp4 = tmp3 - tmp0
tmp5 = 0.75
tmp6 = tmp4 * tmp5
tmp7 = tmp2 + tmp6
tmp8 = -tmp7
tmp10 = 1e-07
tmp11 = triton_helpers.maximum(tmp9, tmp10)
tmp12 = 0.9999999
tmp13 = triton_helpers.minimum(tmp11, tmp12)
tmp14 = tl_math.log(tmp13)
tmp15 = tmp0 * tmp14
tmp16 = tmp3 - tmp13
tmp17 = tl_math.log(tmp16)
tmp18 = tmp4 * tmp17
tmp19 = tmp15 + tmp18
tmp20 = -tmp19
tmp21 = -tmp20
tmp22 = tmp8 * tmp21
tmp23 = tl_math.exp(tmp21)
tmp24 = tmp3 - tmp23
tmp25 = tmp24 * tmp24
tmp26 = tmp22 * tmp25
tmp27 = tl.broadcast_to(tmp26, [RBLOCK])
tmp29 = triton_helpers.promote_to_tensor(tl.sum(tmp27, 0))
tmp30 = tmp9 * tmp0
tmp31 = tl.broadcast_to(tmp30, [RBLOCK])
tmp33 = triton_helpers.promote_to_tensor(tl.sum(tmp31, 0))
tmp34 = tl.broadcast_to(tmp9, [RBLOCK])
tmp36 = triton_helpers.promote_to_tensor(tl.sum(tmp34, 0))
tmp37 = tl.broadcast_to(tmp0, [RBLOCK])
tmp39 = triton_helpers.promote_to_tensor(tl.sum(tmp37, 0))
tmp40 = triton_helpers.maximum(tmp29, tmp10)
tmp41 = tl_math.log(tmp40)
tmp42 = 2.0
tmp43 = tmp33 * tmp42
tmp44 = tmp43 + tmp3
tmp45 = -tmp44
tmp46 = tmp36 + tmp39
tmp47 = tmp46 + tmp3
tmp48 = tmp45 / tmp47
tmp49 = -tmp48
tmp50 = triton_helpers.maximum(tmp49, tmp10)
tmp51 = tl_math.log(tmp50)
tmp52 = tmp51 * tmp3
tmp53 = tmp41 - tmp52
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp53, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf4 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_clamp_div_exp_log_mul_neg_pow_rsub_sub_sum_0[grid
(1)](buf4, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf4,
class DiceLoss(nn.Module):
"""DiceLoss.
.. seealso::
Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for
volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016.
Args:
smooth (float): Value to avoid division by zero when images and predictions are empty.
Attributes:
smooth (float): Value to avoid division by zero when images and predictions are empty.
"""
def __init__(self, smooth=1.0):
super(DiceLoss, self).__init__()
self.smooth = smooth
def forward(self, prediction, target):
iflat = prediction.reshape(-1)
tflat = target.reshape(-1)
intersection = (iflat * tflat).sum()
return -(2.0 * intersection + self.smooth) / (iflat.sum() + tflat.
sum() + self.smooth)
class FocalLoss(nn.Module):
"""FocalLoss.
.. seealso::
Lin, Tsung-Yi, et al. "Focal loss for dense object detection."
Proceedings of the IEEE international conference on computer vision. 2017.
Args:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
Attributes:
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
eps (float): Epsilon to avoid division by zero.
"""
def __init__(self, gamma=2, alpha=0.25, eps=1e-07):
super(FocalLoss, self).__init__()
self.gamma = gamma
self.alpha = alpha
self.eps = eps
def forward(self, input, target):
input = input.clamp(self.eps, 1.0 - self.eps)
cross_entropy = -(target * torch.log(input) + (1 - target) * torch.
log(1 - input))
logpt = -cross_entropy
pt = torch.exp(logpt)
at = self.alpha * target + (1 - self.alpha) * (1 - target)
balanced_cross_entropy = -at * logpt
focal_loss = balanced_cross_entropy * (1 - pt) ** self.gamma
return focal_loss.sum()
class FocalDiceLossNew(nn.Module):
"""FocalDiceLoss.
.. seealso::
Wong, Ken CL, et al. "3D segmentation with exponential logarithmic loss for highly unbalanced object sizes."
International Conference on Medical Image Computing and Computer-Assisted Intervention. Springer, Cham, 2018.
Args:
beta (float): Value from 0 to 1, indicating the weight of the dice loss.
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
Attributes:
beta (float): Value from 0 to 1, indicating the weight of the dice loss.
gamma (float): Value from 0 to 5, Control between easy background and hard ROI
training examples. If set to 0, equivalent to cross-entropy.
alpha (float): Value from 0 to 1, usually corresponding to the inverse of class frequency to address class
imbalance.
"""
def __init__(self, beta=1, gamma=2, alpha=0.25):
super().__init__()
self.beta = beta
self.focal = FocalLoss(gamma, alpha)
self.dice = DiceLoss()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
FocalDiceLoss
| false
| 15,656
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
AttBahdanau
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AttBahdanau(torch.nn.Module):
"""
AttBahdanau: Attention according to Bahdanau that can be used by the
Alignment module.
"""
def __init__(self, q_dim, y_dim, att_dim=128):
super().__init__()
self.q_dim = q_dim
self.y_dim = y_dim
self.att_dim = att_dim
self.Wq = nn.Linear(self.q_dim, self.att_dim)
self.Wy = nn.Linear(self.y_dim, self.att_dim)
self.v = nn.Linear(self.att_dim, 1)
def forward(self, query, y):
att = torch.tanh(self.Wq(query).unsqueeze(1) + self.Wy(y).unsqueeze(2))
att = self.v(att).squeeze(3).transpose(2, 1)
sim = att.max(2)[0].unsqueeze(1)
att = F.softmax(att, dim=2)
return att, sim
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'q_dim': 4, 'y_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_add_tanh_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x4 = xindex // 8192
x5 = xindex % 2048
x0 = xindex % 128
x6 = xindex % 512
x7 = xindex // 2048
x8 = xindex
tmp0 = tl.load(in_ptr0 + (x5 + 2048 * x4), None, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + (x6 + 512 * x7), None, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr3 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp7 = libdevice.tanh(tmp6)
tl.store(out_ptr0 + x8, tmp7, None)
@triton.jit
def triton_poi_fused_max_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 % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
tmp17 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
tmp32 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
tmp2 = tmp0 > tmp1
tmp3 = tmp0 == tmp1
tmp4 = tmp0 != tmp0
tmp5 = tmp1 != tmp1
tmp6 = tmp4 > tmp5
tmp7 = tmp2 | tmp6
tmp8 = tmp4 & tmp5
tmp9 = tmp3 | tmp8
tmp10 = tl.full([1], 0, tl.int64)
tmp11 = tl.full([1], 1, tl.int64)
tmp12 = tmp10 < tmp11
tmp13 = tmp9 & tmp12
tmp14 = tmp7 | tmp13
tmp15 = tl.where(tmp14, tmp0, tmp1)
tmp16 = tl.where(tmp14, tmp10, tmp11)
tmp18 = tmp15 > tmp17
tmp19 = tmp15 == tmp17
tmp20 = tmp15 != tmp15
tmp21 = tmp17 != tmp17
tmp22 = tmp20 > tmp21
tmp23 = tmp18 | tmp22
tmp24 = tmp20 & tmp21
tmp25 = tmp19 | tmp24
tmp26 = tl.full([1], 2, tl.int64)
tmp27 = tmp16 < tmp26
tmp28 = tmp25 & tmp27
tmp29 = tmp23 | tmp28
tmp30 = tl.where(tmp29, tmp15, tmp17)
tmp31 = tl.where(tmp29, tmp16, tmp26)
tmp33 = tmp30 > tmp32
tmp34 = tmp30 == tmp32
tmp35 = tmp30 != tmp30
tmp36 = tmp32 != tmp32
tmp37 = tmp35 > tmp36
tmp38 = tmp33 | tmp37
tmp39 = tmp35 & tmp36
tmp40 = tmp34 | tmp39
tmp41 = tl.full([1], 3, tl.int64)
tmp42 = tmp31 < tmp41
tmp43 = tmp40 & tmp42
tmp44 = tmp38 | tmp43
tl.where(tmp44, tmp30, tmp32)
tmp46 = tl.where(tmp44, tmp31, tmp41)
tmp47 = triton_helpers.maximum(tmp0, tmp1)
tmp48 = triton_helpers.maximum(tmp47, tmp17)
tmp49 = triton_helpers.maximum(tmp48, tmp32)
tl.store(out_ptr0 + x2, tmp46, xmask)
tl.store(out_ptr1 + x2, tmp49, 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 = 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_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4 % 4
x2 = xindex // 16 % 4
x3 = xindex // 64
x4 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x4, 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, (128, 4), (4, 1))
assert_size_stride(primals_2, (128,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (128, 4), (4, 1))
assert_size_stride(primals_5, (128,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_7, (1, 128), (128, 1))
assert_size_stride(primals_8, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 128), (128, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 128), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((64, 128), (128, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 128), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((4, 4, 4, 4, 128), (8192, 2048, 512, 128,
1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_tanh_0[grid(32768)](buf0, primals_2, buf1,
primals_5, buf2, 32768, XBLOCK=128, num_warps=4, num_stages=1)
del buf0
del buf1
del primals_2
del primals_5
buf4 = empty_strided_cuda((256, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_8, reinterpret_tensor(buf2, (256, 128),
(128, 1), 0), reinterpret_tensor(primals_7, (128, 1), (1, 128),
0), alpha=1, beta=1, out=buf4)
del primals_8
buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.int64)
buf6 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
triton_poi_fused_max_1[grid(64)](buf4, buf5, buf6, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf7 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 4, 16, 1, 256),
torch.float32)
triton_poi_fused__softmax_2[grid(256)](buf4, buf7, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf8 = reinterpret_tensor(buf4, (4, 4, 4, 4, 1), (64, 16, 4, 1, 1), 0)
del buf4
triton_poi_fused__softmax_3[grid(256)](buf7, buf8, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del buf7
return buf8, reinterpret_tensor(buf6, (4, 1, 4, 4, 1), (16, 16, 4, 1, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0
), buf2, buf8, reinterpret_tensor(buf5, (4, 4, 1, 4, 1), (16, 4, 4,
1, 1), 0), primals_7
class AttBahdanauNew(torch.nn.Module):
"""
AttBahdanau: Attention according to Bahdanau that can be used by the
Alignment module.
"""
def __init__(self, q_dim, y_dim, att_dim=128):
super().__init__()
self.q_dim = q_dim
self.y_dim = y_dim
self.att_dim = att_dim
self.Wq = nn.Linear(self.q_dim, self.att_dim)
self.Wy = nn.Linear(self.y_dim, self.att_dim)
self.v = nn.Linear(self.att_dim, 1)
def forward(self, input_0, input_1):
primals_1 = self.Wq.weight
primals_2 = self.Wq.bias
primals_4 = self.Wy.weight
primals_5 = self.Wy.bias
primals_7 = self.v.weight
primals_8 = self.v.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], output[1]
|
ishine/NISQA
|
AttBahdanau
| false
| 15,657
|
[
"MIT"
] | 223
|
2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
https://github.com/ishine/NISQA/tree/2c8917f30c4e4bbca3a48e9852301f1e2480a741
|
compute_transform_losses
|
import torch
import torch.nn as nn
import torch.nn.functional as F
def _gather_feat(feat, ind, mask=None):
dim = feat.size(2)
ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim)
feat = feat.gather(1, ind)
if mask is not None:
mask = mask.unsqueeze(2).expand_as(feat)
feat = feat[mask]
feat = feat.view(-1, dim)
return feat
def _transpose_and_gather_feat(feat, ind):
feat = feat.permute(0, 2, 3, 1).contiguous()
feat = feat.view(feat.size(0), -1, feat.size(3))
feat = _gather_feat(feat, ind)
return feat
class L1Loss(nn.Module):
def __init__(self):
super(L1Loss, self).__init__()
def forward(self, output, mask, ind, target):
pred = _transpose_and_gather_feat(output, ind)
mask = mask.unsqueeze(2).expand_as(pred).float()
loss = F.l1_loss(pred * mask, target * mask, size_average=False)
loss = loss / (mask.sum() + 0.0001)
return loss
class compute_transform_losses(nn.Module):
def __init__(self, device='GPU'):
super(compute_transform_losses, self).__init__()
self.device = device
self.l1_loss = L1Loss()
def forward(self, outputs, retransform_output):
loss = F.l1_loss(outputs, retransform_output, size_average=False)
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_abs_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = tmp0 - tmp1
tmp3 = tl_math.abs(tmp2)
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp6, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
get_raw_stream(0)
triton_per_fused_abs_sub_sum_0[grid(1)](arg1_1, arg0_1, buf0, 1,
256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf0,
def _gather_feat(feat, ind, mask=None):
dim = feat.size(2)
ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim)
feat = feat.gather(1, ind)
if mask is not None:
mask = mask.unsqueeze(2).expand_as(feat)
feat = feat[mask]
feat = feat.view(-1, dim)
return feat
def _transpose_and_gather_feat(feat, ind):
feat = feat.permute(0, 2, 3, 1).contiguous()
feat = feat.view(feat.size(0), -1, feat.size(3))
feat = _gather_feat(feat, ind)
return feat
class L1Loss(nn.Module):
def __init__(self):
super(L1Loss, self).__init__()
def forward(self, output, mask, ind, target):
pred = _transpose_and_gather_feat(output, ind)
mask = mask.unsqueeze(2).expand_as(pred).float()
loss = F.l1_loss(pred * mask, target * mask, size_average=False)
loss = loss / (mask.sum() + 0.0001)
return loss
class compute_transform_lossesNew(nn.Module):
def __init__(self, device='GPU'):
super(compute_transform_lossesNew, self).__init__()
self.device = device
self.l1_loss = L1Loss()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
jaidevshriram/cross-view
|
compute_transform_losses
| false
| 15,658
|
[
"MIT"
] | 75
|
844b4ded335e31fe3144adb412792221703d5246
|
https://github.com/jaidevshriram/cross-view/tree/844b4ded335e31fe3144adb412792221703d5246
|
FocalTverskyLoss
|
import torch
import torch.nn as nn
class TverskyLoss(nn.Module):
"""Tversky Loss.
.. seealso::
Salehi, Seyed Sadegh Mohseni, Deniz Erdogmus, and Ali Gholipour. "Tversky loss function for image segmentation
using 3D fully convolutional deep networks." International Workshop on Machine Learning in Medical Imaging.
Springer, Cham, 2017.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Notes:
- setting alpha=beta=0.5: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1706.05721.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, smooth=1.0):
super(TverskyLoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.smooth = smooth
def tversky_index(self, y_pred, y_true):
"""Compute Tversky index.
Args:
y_pred (torch Tensor): Prediction.
y_true (torch Tensor): Target.
Returns:
float: Tversky index.
"""
y_true = y_true.float()
tp = torch.sum(y_true * y_pred)
fn = torch.sum(y_true * (1 - y_pred))
fp = torch.sum((1 - y_true) * y_pred)
numerator = tp + self.smooth
denominator = tp + self.alpha * fp + self.beta * fn + self.smooth
tversky_label = numerator / denominator
return tversky_label
def forward(self, input, target):
n_classes = input.shape[1]
tversky_sum = 0.0
for i_label in range(n_classes):
y_pred, y_true = input[:, i_label], target[:, i_label]
tversky_sum += self.tversky_index(y_pred, y_true)
return -tversky_sum / n_classes
class FocalTverskyLoss(TverskyLoss):
"""Focal Tversky Loss.
.. seealso::
Abraham, Nabila, and Naimul Mefraz Khan. "A novel focal tversky loss function with improved attention u-net for
lesion segmentation." 2019 IEEE 16th International Symposium on Biomedical Imaging (ISBI 2019). IEEE, 2019.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
gamma (float): Typically between 1 and 3. Control between easy background and hard ROI training examples.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
gamma (float): Typically between 1 and 3. Control between easy background and hard ROI training examples.
Notes:
- setting alpha=beta=0.5 and gamma=1: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1810.07842.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, gamma=1.33, smooth=1.0):
super(FocalTverskyLoss, self).__init__()
self.gamma = gamma
self.tversky = TverskyLoss(alpha=alpha, beta=beta, smooth=smooth)
def forward(self, input, target):
n_classes = input.shape[1]
focal_tversky_sum = 0.0
for i_label in range(n_classes):
y_pred, y_true = input[:, i_label], target[:, i_label]
tversky_index = self.tversky.tversky_index(y_pred, y_true)
focal_tversky_sum += torch.pow(1 - tversky_index, exponent=1 /
self.gamma)
return focal_tversky_sum / n_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
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_add_div_mul_pow_rsub_sum_0(in_out_ptr1, in_ptr0,
in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex % 16
r1 = rindex // 16
tmp0 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
tmp1 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
tmp17 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
tmp18 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
tmp33 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
tmp34 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
tmp49 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
tmp50 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
tmp2 = tmp0 * tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp5 = tl.sum(tmp3, 1)[:, None]
tmp6 = 1.0
tmp7 = tmp6 - tmp0
tmp8 = tmp7 * tmp1
tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
tmp11 = tl.sum(tmp9, 1)[:, None]
tmp12 = tmp6 - tmp1
tmp13 = tmp0 * tmp12
tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
tmp16 = tl.sum(tmp14, 1)[:, None]
tmp19 = tmp17 * tmp18
tmp20 = tl.broadcast_to(tmp19, [XBLOCK, RBLOCK])
tmp22 = tl.sum(tmp20, 1)[:, None]
tmp23 = tmp6 - tmp17
tmp24 = tmp23 * tmp18
tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
tmp27 = tl.sum(tmp25, 1)[:, None]
tmp28 = tmp6 - tmp18
tmp29 = tmp17 * tmp28
tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
tmp32 = tl.sum(tmp30, 1)[:, None]
tmp35 = tmp33 * tmp34
tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
tmp38 = tl.sum(tmp36, 1)[:, None]
tmp39 = tmp6 - tmp33
tmp40 = tmp39 * tmp34
tmp41 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
tmp43 = tl.sum(tmp41, 1)[:, None]
tmp44 = tmp6 - tmp34
tmp45 = tmp33 * tmp44
tmp46 = tl.broadcast_to(tmp45, [XBLOCK, RBLOCK])
tmp48 = tl.sum(tmp46, 1)[:, None]
tmp51 = tmp49 * tmp50
tmp52 = tl.broadcast_to(tmp51, [XBLOCK, RBLOCK])
tmp54 = tl.sum(tmp52, 1)[:, None]
tmp55 = tmp6 - tmp49
tmp56 = tmp55 * tmp50
tmp57 = tl.broadcast_to(tmp56, [XBLOCK, RBLOCK])
tmp59 = tl.sum(tmp57, 1)[:, None]
tmp60 = tmp6 - tmp50
tmp61 = tmp49 * tmp60
tmp62 = tl.broadcast_to(tmp61, [XBLOCK, RBLOCK])
tmp64 = tl.sum(tmp62, 1)[:, None]
tmp65 = tmp22 + tmp6
tmp66 = 0.7
tmp67 = tmp27 * tmp66
tmp68 = tmp22 + tmp67
tmp69 = 0.3
tmp70 = tmp32 * tmp69
tmp71 = tmp68 + tmp70
tmp72 = tmp71 + tmp6
tmp73 = tmp65 / tmp72
tmp74 = tmp6 - tmp73
tmp75 = 0.7518796992481203
tmp76 = libdevice.pow(tmp74, tmp75)
tmp77 = 0.0
tmp78 = tmp76 + tmp77
tmp79 = tmp54 + tmp6
tmp80 = tmp59 * tmp66
tmp81 = tmp54 + tmp80
tmp82 = tmp64 * tmp69
tmp83 = tmp81 + tmp82
tmp84 = tmp83 + tmp6
tmp85 = tmp79 / tmp84
tmp86 = tmp6 - tmp85
tmp87 = libdevice.pow(tmp86, tmp75)
tmp88 = tmp78 + tmp87
tmp89 = tmp5 + tmp6
tmp90 = tmp11 * tmp66
tmp91 = tmp5 + tmp90
tmp92 = tmp16 * tmp69
tmp93 = tmp91 + tmp92
tmp94 = tmp93 + tmp6
tmp95 = tmp89 / tmp94
tmp96 = tmp6 - tmp95
tmp97 = libdevice.pow(tmp96, tmp75)
tmp98 = tmp88 + tmp97
tmp99 = tmp38 + tmp6
tmp100 = tmp43 * tmp66
tmp101 = tmp38 + tmp100
tmp102 = tmp48 * tmp69
tmp103 = tmp101 + tmp102
tmp104 = tmp103 + tmp6
tmp105 = tmp99 / tmp104
tmp106 = tmp6 - tmp105
tmp107 = libdevice.pow(tmp106, tmp75)
tmp108 = tmp98 + tmp107
tmp109 = 0.25
tmp110 = tmp108 * tmp109
tl.debug_barrier()
tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp110, 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)
buf10 = empty_strided_cuda((), (), torch.float32)
buf13 = buf10
del buf10
buf14 = buf13
del buf13
get_raw_stream(0)
triton_per_fused_add_div_mul_pow_rsub_sum_0[grid(1)](buf14, arg1_1,
arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf14,
class TverskyLoss(nn.Module):
"""Tversky Loss.
.. seealso::
Salehi, Seyed Sadegh Mohseni, Deniz Erdogmus, and Ali Gholipour. "Tversky loss function for image segmentation
using 3D fully convolutional deep networks." International Workshop on Machine Learning in Medical Imaging.
Springer, Cham, 2017.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Notes:
- setting alpha=beta=0.5: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1706.05721.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, smooth=1.0):
super(TverskyLoss, self).__init__()
self.alpha = alpha
self.beta = beta
self.smooth = smooth
def tversky_index(self, y_pred, y_true):
"""Compute Tversky index.
Args:
y_pred (torch Tensor): Prediction.
y_true (torch Tensor): Target.
Returns:
float: Tversky index.
"""
y_true = y_true.float()
tp = torch.sum(y_true * y_pred)
fn = torch.sum(y_true * (1 - y_pred))
fp = torch.sum((1 - y_true) * y_pred)
numerator = tp + self.smooth
denominator = tp + self.alpha * fp + self.beta * fn + self.smooth
tversky_label = numerator / denominator
return tversky_label
def forward(self, input, target):
n_classes = input.shape[1]
tversky_sum = 0.0
for i_label in range(n_classes):
y_pred, y_true = input[:, i_label], target[:, i_label]
tversky_sum += self.tversky_index(y_pred, y_true)
return -tversky_sum / n_classes
class FocalTverskyLossNew(TverskyLoss):
"""Focal Tversky Loss.
.. seealso::
Abraham, Nabila, and Naimul Mefraz Khan. "A novel focal tversky loss function with improved attention u-net for
lesion segmentation." 2019 IEEE 16th International Symposium on Biomedical Imaging (ISBI 2019). IEEE, 2019.
Args:
alpha (float): Weight of false positive voxels.
beta (float): Weight of false negative voxels.
gamma (float): Typically between 1 and 3. Control between easy background and hard ROI training examples.
smooth (float): Epsilon to avoid division by zero, when both Numerator and Denominator of Tversky are zeros.
Attributes:
gamma (float): Typically between 1 and 3. Control between easy background and hard ROI training examples.
Notes:
- setting alpha=beta=0.5 and gamma=1: Equivalent to DiceLoss.
- default parameters were suggested by https://arxiv.org/pdf/1810.07842.pdf .
"""
def __init__(self, alpha=0.7, beta=0.3, gamma=1.33, smooth=1.0):
super(FocalTverskyLossNew, self).__init__()
self.gamma = gamma
self.tversky = TverskyLoss(alpha=alpha, beta=beta, smooth=smooth)
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ivadomed-profile-analysis-project/ivadomed
|
FocalTverskyLoss
| false
| 15,659
|
[
"MIT"
] | 87
|
3b53e2cb2b210511943da439401e2471fd387876
|
https://github.com/ivadomed-profile-analysis-project/ivadomed/tree/3b53e2cb2b210511943da439401e2471fd387876
|
BertImagePooler
|
from _paritybench_helpers import _mock_config
import torch
import torch.nn as nn
class BertImagePooler(nn.Module):
def __init__(self, config):
super(BertImagePooler, self).__init__()
self.dense = nn.Linear(config.v_hidden_size, config.bi_hidden_size)
self.activation = nn.ReLU()
def forward(self, hidden_states):
first_token_tensor = hidden_states[:, 0]
pooled_output = self.dense(first_token_tensor)
pooled_output = self.activation(pooled_output)
return pooled_output
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'config': _mock_config(v_hidden_size=4, bi_hidden_size=4)}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tl.store(out_ptr0 + x2, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_relu_threshold_backward_1(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(64)](primals_1, buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_1
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.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
buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
triton_poi_fused_add_relu_threshold_backward_1[grid(64)](buf2,
primals_3, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return buf2, reinterpret_tensor(buf0, (16, 4), (4, 1), 0), buf3
class BertImagePoolerNew(nn.Module):
def __init__(self, config):
super(BertImagePoolerNew, self).__init__()
self.dense = nn.Linear(config.v_hidden_size, config.bi_hidden_size)
self.activation = nn.ReLU()
def forward(self, input_0):
primals_2 = self.dense.weight
primals_3 = self.dense.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
BigRedT/gpv-1
|
BertImagePooler
| false
| 15,660
|
[
"Apache-2.0"
] | 45
|
6a0c2173b44961cb492d00f94864c461aa77641d
|
https://github.com/BigRedT/gpv-1/tree/6a0c2173b44961cb492d00f94864c461aa77641d
|
AdditiveAttention
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AdditiveAttention(nn.Module):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(AdditiveAttention, self).__init__()
if internal_dim is None:
internal_dim = int((encoder_hidden_state_dim +
decoder_hidden_state_dim) / 2)
self.w1 = nn.Linear(encoder_hidden_state_dim, internal_dim, bias=False)
self.w2 = nn.Linear(decoder_hidden_state_dim, internal_dim, bias=False)
self.v = nn.Linear(internal_dim, 1, bias=False)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + self.w2(
decoder_state)))
def forward(self, encoder_states, decoder_state):
score_vec = torch.cat([self.score(encoder_states[:, i],
decoder_state) for i in range(encoder_states.shape[1])], dim=1)
attention_probs = torch.unsqueeze(F.softmax(score_vec, dim=1), dim=2)
final_context_vec = torch.sum(attention_probs * encoder_states, dim=1)
return final_context_vec, attention_probs
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'encoder_hidden_state_dim': 4, 'decoder_hidden_state_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
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_mm_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_mm_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_mm_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_mm_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_tanh_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tmp5 = tmp4 + tmp1
tmp6 = libdevice.tanh(tmp5)
tmp8 = tmp7 + tmp1
tmp9 = libdevice.tanh(tmp8)
tmp11 = tmp10 + tmp1
tmp12 = libdevice.tanh(tmp11)
tl.store(out_ptr0 + x2, tmp3, xmask)
tl.store(out_ptr1 + x2, tmp6, xmask)
tl.store(out_ptr2 + x2, tmp9, xmask)
tl.store(out_ptr3 + x2, tmp12, xmask)
@triton.jit
def triton_poi_fused__softmax_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
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_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 = 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_7(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 + 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, 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, (1, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mm_0[grid(4)](primals_1, buf0, 4, XBLOCK=4,
num_warps=1, num_stages=1)
buf1 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
extern_kernels.mm(buf0, reinterpret_tensor(primals_2, (4, 4), (1, 4
), 0), out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_4, reinterpret_tensor(primals_3, (4, 4),
(1, 4), 0), out=buf2)
del primals_3
buf10 = buf0
del buf0
triton_poi_fused_mm_1[grid(4)](primals_1, buf10, 4, XBLOCK=4,
num_warps=1, num_stages=1)
buf11 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
extern_kernels.mm(buf10, reinterpret_tensor(primals_2, (4, 4), (1,
4), 0), out=buf11)
buf4 = buf10
del buf10
triton_poi_fused_mm_2[grid(4)](primals_1, buf4, 4, XBLOCK=4,
num_warps=1, num_stages=1)
buf5 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
extern_kernels.mm(buf4, reinterpret_tensor(primals_2, (4, 4), (1, 4
), 0), out=buf5)
buf7 = buf4
del buf4
triton_poi_fused_mm_3[grid(4)](primals_1, buf7, 4, XBLOCK=4,
num_warps=1, num_stages=1)
buf8 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
extern_kernels.mm(buf7, reinterpret_tensor(primals_2, (4, 4), (1, 4
), 0), out=buf8)
del buf7
del primals_2
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_tanh_4[grid(16)](buf1, buf2, buf5, buf8, buf11,
buf3, buf6, buf9, buf12, 16, XBLOCK=16, num_warps=1, num_stages=1)
del buf1
del buf11
del buf5
del buf8
buf17 = buf2
del buf2
buf13 = reinterpret_tensor(buf17, (4, 1), (4, 1), 0)
extern_kernels.mm(buf3, reinterpret_tensor(primals_5, (4, 1), (1, 4
), 0), out=buf13)
buf14 = reinterpret_tensor(buf17, (4, 1), (4, 1), 1)
extern_kernels.mm(buf6, reinterpret_tensor(primals_5, (4, 1), (1, 4
), 0), out=buf14)
buf15 = reinterpret_tensor(buf17, (4, 1), (4, 1), 2)
extern_kernels.mm(buf9, reinterpret_tensor(primals_5, (4, 1), (1, 4
), 0), out=buf15)
buf16 = reinterpret_tensor(buf17, (4, 1), (4, 1), 3)
extern_kernels.mm(buf12, reinterpret_tensor(primals_5, (4, 1), (1,
4), 0), out=buf16)
buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused__softmax_5[grid(16)](buf17, buf18, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf13
del buf14
del buf15
del buf16
buf19 = buf17
del buf17
triton_poi_fused__softmax_6[grid(16)](buf18, buf19, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf20 = buf18
del buf18
triton_poi_fused_mul_sum_7[grid(16)](buf19, primals_1, buf20, 16,
XBLOCK=16, num_warps=1, num_stages=1)
return buf20, reinterpret_tensor(buf19, (4, 4, 1), (4, 1, 1), 0
), primals_1, primals_4, reinterpret_tensor(primals_1, (1, 4), (16,
4), 0), buf3, reinterpret_tensor(primals_1, (1, 4), (16, 4), 1
), buf6, reinterpret_tensor(primals_1, (1, 4), (16, 4), 2
), buf9, reinterpret_tensor(primals_1, (1, 4), (16, 4), 3
), buf12, buf19, primals_5
class AdditiveAttentionNew(nn.Module):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(AdditiveAttentionNew, self).__init__()
if internal_dim is None:
internal_dim = int((encoder_hidden_state_dim +
decoder_hidden_state_dim) / 2)
self.w1 = nn.Linear(encoder_hidden_state_dim, internal_dim, bias=False)
self.w2 = nn.Linear(decoder_hidden_state_dim, internal_dim, bias=False)
self.v = nn.Linear(internal_dim, 1, bias=False)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + self.w2(
decoder_state)))
def forward(self, input_0, input_1):
primals_1 = self.w1.weight
primals_2 = self.w2.weight
primals_5 = self.v.weight
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0], output[1]
|
j-scharrenbach/Trajectron-plus-plus
|
AdditiveAttention
| false
| 15,661
|
[
"MIT"
] | 361
|
37040ca6e3f386c80ab39fbb4aa9984915c94813
|
https://github.com/j-scharrenbach/Trajectron-plus-plus/tree/37040ca6e3f386c80ab39fbb4aa9984915c94813
|
TemporallyBatchedAdditiveAttention
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class AdditiveAttention(nn.Module):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(AdditiveAttention, self).__init__()
if internal_dim is None:
internal_dim = int((encoder_hidden_state_dim +
decoder_hidden_state_dim) / 2)
self.w1 = nn.Linear(encoder_hidden_state_dim, internal_dim, bias=False)
self.w2 = nn.Linear(decoder_hidden_state_dim, internal_dim, bias=False)
self.v = nn.Linear(internal_dim, 1, bias=False)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + self.w2(
decoder_state)))
def forward(self, encoder_states, decoder_state):
score_vec = torch.cat([self.score(encoder_states[:, i],
decoder_state) for i in range(encoder_states.shape[1])], dim=1)
attention_probs = torch.unsqueeze(F.softmax(score_vec, dim=1), dim=2)
final_context_vec = torch.sum(attention_probs * encoder_states, dim=1)
return final_context_vec, attention_probs
class TemporallyBatchedAdditiveAttention(AdditiveAttention):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(TemporallyBatchedAdditiveAttention, self).__init__(
encoder_hidden_state_dim, decoder_hidden_state_dim, internal_dim)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + torch.unsqueeze(
self.w2(decoder_state), dim=1)))
def forward(self, encoder_states, decoder_state):
score_vec = self.score(encoder_states, decoder_state)
attention_probs = F.softmax(score_vec, dim=1)
final_context_vec = torch.sum(attention_probs * encoder_states, dim=1)
return final_context_vec, torch.squeeze(torch.transpose(
attention_probs, 1, 2), dim=3)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'encoder_hidden_state_dim': 4, 'decoder_hidden_state_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_tanh_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
x3 = xindex % 256
x0 = xindex % 64
x2 = xindex // 256
x4 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(out_ptr0 + x4, tmp3, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
@triton.jit
def triton_poi_fused_mul_sum_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4 % 16
x2 = xindex // 64
x3 = xindex % 64
x4 = xindex
tmp0 = tl.load(in_ptr0 + (x1 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (16 + x1 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr1 + (64 + x3), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (32 + x1 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr1 + (128 + x3), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (48 + x1 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp12 = tl.load(in_ptr1 + (192 + x3), 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 + x4, tmp14, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_5, (1, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1)
del primals_3
buf2 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_add_tanh_0[grid(1024)](buf0, buf1, buf2, 1024,
XBLOCK=256, num_warps=4, num_stages=1)
buf3 = reinterpret_tensor(buf1, (256, 1), (1, 1), 0)
del buf1
extern_kernels.mm(reinterpret_tensor(buf2, (256, 4), (4, 1), 0),
reinterpret_tensor(primals_5, (4, 1), (1, 4), 0), out=buf3)
buf4 = reinterpret_tensor(buf0, (4, 4, 4, 4, 1), (64, 16, 4, 1, 256), 0
)
del buf0
triton_poi_fused__softmax_1[grid(256)](buf3, buf4, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf5 = reinterpret_tensor(buf3, (4, 4, 4, 4, 1), (64, 16, 4, 1, 1), 0)
del buf3
triton_poi_fused__softmax_2[grid(256)](buf4, buf5, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf6 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf4
triton_poi_fused_mul_sum_3[grid(256)](buf5, primals_2, buf6, 256,
XBLOCK=256, num_warps=4, num_stages=1)
return buf6, reinterpret_tensor(buf5, (4, 4, 4, 4, 1), (64, 4, 16, 1, 1), 0
), primals_2, reinterpret_tensor(primals_4, (64, 4), (4, 1), 0
), buf2, buf5, primals_5
class AdditiveAttention(nn.Module):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(AdditiveAttention, self).__init__()
if internal_dim is None:
internal_dim = int((encoder_hidden_state_dim +
decoder_hidden_state_dim) / 2)
self.w1 = nn.Linear(encoder_hidden_state_dim, internal_dim, bias=False)
self.w2 = nn.Linear(decoder_hidden_state_dim, internal_dim, bias=False)
self.v = nn.Linear(internal_dim, 1, bias=False)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + self.w2(
decoder_state)))
def forward(self, encoder_states, decoder_state):
score_vec = torch.cat([self.score(encoder_states[:, i],
decoder_state) for i in range(encoder_states.shape[1])], dim=1)
attention_probs = torch.unsqueeze(F.softmax(score_vec, dim=1), dim=2)
final_context_vec = torch.sum(attention_probs * encoder_states, dim=1)
return final_context_vec, attention_probs
class TemporallyBatchedAdditiveAttentionNew(AdditiveAttention):
def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
internal_dim=None):
super(TemporallyBatchedAdditiveAttentionNew, self).__init__(
encoder_hidden_state_dim, decoder_hidden_state_dim, internal_dim)
def score(self, encoder_state, decoder_state):
return self.v(torch.tanh(self.w1(encoder_state) + torch.unsqueeze(
self.w2(decoder_state), dim=1)))
def forward(self, input_0, input_1):
primals_1 = self.w1.weight
primals_3 = self.w2.weight
primals_5 = self.v.weight
primals_2 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0], output[1]
|
j-scharrenbach/Trajectron-plus-plus
|
TemporallyBatchedAdditiveAttention
| false
| 15,662
|
[
"MIT"
] | 361
|
37040ca6e3f386c80ab39fbb4aa9984915c94813
|
https://github.com/j-scharrenbach/Trajectron-plus-plus/tree/37040ca6e3f386c80ab39fbb4aa9984915c94813
|
SeqToSeqAtten
|
import torch
import torch.utils.data
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class SeqToSeqAtten(torch.nn.Module):
"""
Args:
-
Inputs:
- h1: (seq1_len, batch, hidden_size)
- h1_mask: (batch, seq1_len)
- h2: (seq2_len, batch, hidden_size)
- h2_mask: (batch, seq2_len)
Outputs:
- output: (seq1_len, batch, hidden_size)
- alpha: (batch, seq1_len, seq2_len)
"""
def __init__(self):
super(SeqToSeqAtten, self).__init__()
def forward(self, h1, h2, h2_mask):
h1 = h1.transpose(0, 1)
h2 = h2.transpose(0, 1)
alpha = h1.bmm(h2.transpose(1, 2))
alpha = masked_softmax(alpha, h2_mask.unsqueeze(1), dim=2)
alpha_seq2 = alpha.bmm(h2)
alpha_seq2 = alpha_seq2.transpose(0, 1)
return alpha_seq2, alpha
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 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
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_exp_max_mul_sub_sum_0(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
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 = triton_helpers.maximum(tmp2, tmp5)
tmp9 = tmp7 * tmp8
tmp10 = triton_helpers.maximum(tmp6, tmp9)
tmp13 = tmp11 * tmp12
tmp14 = triton_helpers.maximum(tmp10, tmp13)
tmp15 = tmp2 - tmp14
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp1
tmp18 = tmp5 - tmp14
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp19 * tmp4
tmp21 = tmp17 + tmp20
tmp22 = tmp9 - tmp14
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp23 * tmp8
tmp25 = tmp21 + tmp24
tmp26 = tmp13 - tmp14
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp27 * tmp12
tmp29 = tmp25 + tmp28
tl.store(out_ptr0 + x2, tmp14, xmask)
tl.store(out_ptr1 + x2, tmp29, xmask)
@triton.jit
def triton_poi_fused_add_div_exp_max_mul_sub_1(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 4
x2 = xindex // 16
x4 = xindex // 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
)
tmp3 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp5 * tmp1
tmp8 = 1e-06
tmp9 = tmp7 + tmp8
tmp10 = tmp6 / tmp9
tl.store(in_out_ptr0 + x3, tmp10, xmask)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(arg2_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 4, 4), (4, 16, 1),
0), reinterpret_tensor(arg1_1, (4, 4, 4), (4, 1, 16), 0), out=buf0)
del arg0_1
buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
buf2 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused_exp_max_mul_sub_sum_0[grid(16)](buf0, arg2_1, buf1,
buf2, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf3 = buf0
del buf0
triton_poi_fused_add_div_exp_max_mul_sub_1[grid(64)](buf3, arg2_1,
buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
del arg2_1
del buf1
del buf2
buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf3, reinterpret_tensor(arg1_1, (4, 4, 4), (4,
16, 1), 0), out=buf4)
del arg1_1
return reinterpret_tensor(buf4, (4, 4, 4), (4, 16, 1), 0), buf3
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class SeqToSeqAttenNew(torch.nn.Module):
"""
Args:
-
Inputs:
- h1: (seq1_len, batch, hidden_size)
- h1_mask: (batch, seq1_len)
- h2: (seq2_len, batch, hidden_size)
- h2_mask: (batch, seq2_len)
Outputs:
- output: (seq1_len, batch, hidden_size)
- alpha: (batch, seq1_len, seq2_len)
"""
def __init__(self):
super(SeqToSeqAttenNew, self).__init__()
def forward(self, input_0, input_1, input_2):
arg0_1 = input_0
arg1_1 = input_1
arg2_1 = input_2
output = call([arg0_1, arg1_1, arg2_1])
return output[0], output[1]
|
jamaalhay/Final_Proj
|
SeqToSeqAtten
| false
| 15,663
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
ConvModule
|
import torch
import torch.utils.data.distributed
from torch import nn
import torch.utils.data
class ConvModule(nn.Module):
def __init__(self, input_dim, kernel_size, dropout_rate, causal=False):
super(ConvModule, self).__init__()
self.layer_norm = nn.LayerNorm(input_dim)
self.pw_conv_1 = nn.Conv2d(1, 2, 1, 1, 0)
self.glu_act = torch.nn.Sigmoid()
self.causal = causal
self.kernel_size = kernel_size
if causal:
self.dw_conv_1d = nn.Conv1d(input_dim, input_dim, kernel_size,
1, padding=kernel_size - 1, groups=input_dim)
else:
self.dw_conv_1d = nn.Conv1d(input_dim, input_dim, kernel_size,
1, padding=(kernel_size - 1) // 2, groups=input_dim)
self.act = nn.ReLU()
self.pw_conv_2 = nn.Conv2d(1, 1, 1, 1, 0)
self.dropout = nn.Dropout(dropout_rate)
def forward(self, x):
x = x.unsqueeze(1)
x = self.layer_norm(x)
x = self.pw_conv_1(x)
x = x[:, 0] * self.glu_act(x[:, 1])
x = x.permute([0, 2, 1])
x = self.dw_conv_1d(x)
if self.causal:
x = x[:, :, :-(self.kernel_size - 1)]
x = self.act(x)
x = x.unsqueeze(1).permute([0, 1, 3, 2])
x = self.pw_conv_2(x)
x = self.dropout(x).squeeze(1)
return x
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'kernel_size': 4, 'dropout_rate': 0.5}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import torch.utils.data.distributed
from torch import nn
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_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
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp7 = 4.0
tmp8 = tmp6 / tmp7
tmp9 = tmp0 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tmp1 - tmp8
tmp12 = tmp11 * tmp11
tmp13 = tmp10 + tmp12
tmp14 = tmp3 - tmp8
tmp15 = tmp14 * tmp14
tmp16 = tmp13 + tmp15
tmp17 = tmp5 - tmp8
tmp18 = tmp17 * tmp17
tmp19 = tmp16 + tmp18
tmp20 = tmp19 / tmp7
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tl.store(out_ptr0 + x0, tmp8, xmask)
tl.store(out_ptr1 + x0, tmp23, xmask)
@triton.jit
def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = tmp2 * tmp3
tmp6 = tmp4 * tmp5
tmp8 = tmp6 + tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 128
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 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)
@triton.jit
def triton_poi_fused_mul_sigmoid_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 % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 32 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (16 + x0 + 32 * x1), xmask)
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x2, tmp3, xmask)
@triton.jit
def triton_poi_fused_convolution_4(in_ptr0, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_convolution_relu_threshold_backward_5(in_out_ptr0,
in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 3 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x3, tmp4, xmask)
tl.store(out_ptr0 + x3, tmp6, xmask)
@triton.jit
def triton_poi_fused_convolution_6(in_ptr0, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 12
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 3
y1 = yindex // 3
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 3 * x2 + 12 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_convolution_7(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 48
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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, primals_8, primals_9) = 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, (2, 1, 1, 1), (1, 1, 1, 1))
assert_size_stride(primals_5, (2,), (1,))
assert_size_stride(primals_6, (4, 1, 4), (4, 4, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (1, 1, 1, 1), (1, 1, 1, 1))
assert_size_stride(primals_9, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 1, 4, 1), (4, 16, 1, 16), torch.float32)
buf1 = empty_strided_cuda((4, 1, 4, 1), (4, 16, 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)
buf2 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.float32)
triton_poi_fused_native_layer_norm_1[grid(64)](primals_1, buf0,
buf1, primals_2, primals_3, buf2, 64, XBLOCK=64, num_warps=1,
num_stages=1)
del buf0
del buf1
del primals_2
del primals_3
buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf3, (4, 2, 4, 4), (32, 16, 4, 1))
buf4 = buf3
del buf3
triton_poi_fused_convolution_2[grid(128)](buf4, primals_5, 128,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_mul_sigmoid_3[grid(64)](buf4, buf5, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_convolution_4[grid(16, 4)](buf5, buf6, 16, 4,
XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
buf7 = extern_kernels.convolution(buf6, primals_6, stride=(1,),
padding=(1,), dilation=(1,), transposed=False, output_padding=(
0,), groups=4, bias=None)
assert_size_stride(buf7, (4, 4, 3), (12, 3, 1))
del buf6
buf8 = buf7
del buf7
buf12 = empty_strided_cuda((4, 4, 3), (12, 3, 1), torch.bool)
triton_poi_fused_convolution_relu_threshold_backward_5[grid(48)](buf8,
primals_7, buf12, 48, XBLOCK=64, num_warps=1, num_stages=1)
del primals_7
buf9 = empty_strided_cuda((4, 1, 3, 4), (12, 12, 4, 1), torch.float32)
triton_poi_fused_convolution_6[grid(12, 4)](buf8, buf9, 12, 4,
XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
buf10 = extern_kernels.convolution(buf9, primals_8, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf10, (4, 1, 3, 4), (12, 12, 4, 1))
del buf9
buf11 = reinterpret_tensor(buf10, (4, 1, 3, 4), (12, 1, 4, 1), 0)
del buf10
triton_poi_fused_convolution_7[grid(48)](buf11, primals_9, 48,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_9
return (reinterpret_tensor(buf11, (4, 3, 4), (12, 4, 1), 0), primals_1,
primals_4, primals_6, primals_8, buf2, buf4, reinterpret_tensor(
buf5, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf8, (4, 1, 3,
4), (12, 12, 1, 3), 0), buf12)
class ConvModuleNew(nn.Module):
def __init__(self, input_dim, kernel_size, dropout_rate, causal=False):
super(ConvModuleNew, self).__init__()
self.layer_norm = nn.LayerNorm(input_dim)
self.pw_conv_1 = nn.Conv2d(1, 2, 1, 1, 0)
self.glu_act = torch.nn.Sigmoid()
self.causal = causal
self.kernel_size = kernel_size
if causal:
self.dw_conv_1d = nn.Conv1d(input_dim, input_dim, kernel_size,
1, padding=kernel_size - 1, groups=input_dim)
else:
self.dw_conv_1d = nn.Conv1d(input_dim, input_dim, kernel_size,
1, padding=(kernel_size - 1) // 2, groups=input_dim)
self.act = nn.ReLU()
self.pw_conv_2 = nn.Conv2d(1, 1, 1, 1, 0)
self.dropout = nn.Dropout(dropout_rate)
def forward(self, input_0):
primals_2 = self.layer_norm.weight
primals_3 = self.layer_norm.bias
primals_4 = self.pw_conv_1.weight
primals_5 = self.pw_conv_1.bias
primals_6 = self.dw_conv_1d.weight
primals_7 = self.dw_conv_1d.bias
primals_8 = self.pw_conv_2.weight
primals_9 = self.pw_conv_2.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]
|
ishine/StreamingTransformer
|
ConvModule
| false
| 15,664
|
[
"Apache-2.0"
] | 252
|
4b56931a311d65686d310c54cc6896a4be4f47de
|
https://github.com/ishine/StreamingTransformer/tree/4b56931a311d65686d310c54cc6896a4be4f47de
|
PointerAttention
|
import torch
import torch.utils.data
import torch.nn.functional as F
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class PointerAttention(torch.nn.Module):
"""
attention mechanism in pointer network
Args:
- input_size: The number of features in Hr
- hidden_size: The number of features in the hidden layer
Inputs:
Hr(context_len, batch, hidden_size * num_directions): question-aware context representation
Hk_last(batch, hidden_size): the last hidden output of previous time
Outputs:
beta(batch, context_len): question-aware context representation
"""
def __init__(self, input_size, hidden_size):
super(PointerAttention, self).__init__()
self.linear_wr = torch.nn.Linear(input_size, hidden_size)
self.linear_wa = torch.nn.Linear(hidden_size, hidden_size)
self.linear_wf = torch.nn.Linear(hidden_size, 1)
def forward(self, Hr, Hr_mask, Hk_pre):
wr_hr = self.linear_wr(Hr)
wa_ha = self.linear_wa(Hk_pre).unsqueeze(0)
f = F.tanh(wr_hr + wa_ha)
beta_tmp = self.linear_wf(f).squeeze(2).transpose(0, 1)
beta = masked_softmax(beta_tmp, m=Hr_mask, dim=1)
return beta
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 [[], {'input_size': 4, 'hidden_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
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_add_tanh_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp7 = libdevice.tanh(tmp6)
tl.store(in_out_ptr0 + x2, tmp7, xmask)
@triton.jit
def triton_poi_fused_add_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex // 4
x4 = xindex % 64
x5 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (64 + x4), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr1 + (128 + x4), xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr1 + (192 + x4), xmask, eviction_policy='evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp0 * tmp3
tmp5 = triton_helpers.maximum(tmp2, tmp4)
tmp7 = tmp0 * tmp6
tmp8 = triton_helpers.maximum(tmp5, tmp7)
tmp10 = tmp0 * tmp9
tmp11 = triton_helpers.maximum(tmp8, tmp10)
tmp12 = tmp2 - tmp11
tmp13 = tl_math.exp(tmp12)
tmp14 = tmp13 * tmp1
tmp15 = tmp4 - tmp11
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp3
tmp18 = tmp14 + tmp17
tmp19 = tmp7 - tmp11
tmp20 = tl_math.exp(tmp19)
tmp21 = tmp20 * tmp6
tmp22 = tmp18 + tmp21
tmp23 = tmp10 - tmp11
tmp24 = tl_math.exp(tmp23)
tmp25 = tmp24 * tmp9
tmp26 = tmp22 + tmp25
tmp27 = 1e-06
tmp28 = tmp26 + tmp27
tl.store(out_ptr0 + x5, tmp11, xmask)
tl.store(out_ptr1 + x5, tmp28, xmask)
@triton.jit
def triton_poi_fused_add_div_exp_max_mul_sub_sum_2(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4 % 16
x3 = xindex // 256
x4 = xindex % 256
x5 = xindex % 64
x6 = xindex
tmp0 = tl.load(in_ptr0 + (x1 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + (x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr3 + (x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp5 * tmp1
tmp8 = tmp6 / tmp7
tl.store(out_ptr0 + x6, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_7, (1, 4), (4, 1))
assert_size_stride(primals_8, (1,), (1,))
assert_size_stride(primals_9, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = reinterpret_tensor(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
)
del buf0
get_raw_stream(0)
triton_poi_fused_add_tanh_0[grid(256)](buf2, primals_2, buf1,
primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
del primals_5
buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_8, reinterpret_tensor(buf2, (64, 4), (
4, 1), 0), reinterpret_tensor(primals_7, (4, 1), (1, 4), 0),
alpha=1, beta=1, out=buf4)
del primals_8
buf5 = reinterpret_tensor(buf1, (4, 1, 4, 4, 4), (64, 256, 16, 4, 1), 0
)
del buf1
buf6 = empty_strided_cuda((4, 1, 4, 4, 4), (64, 256, 16, 4, 1),
torch.float32)
triton_poi_fused_add_exp_max_mul_sub_sum_1[grid(256)](buf4,
primals_9, buf5, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1)
buf7 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_add_div_exp_max_mul_sub_sum_2[grid(1024)](buf4,
primals_9, buf5, buf6, buf7, 1024, XBLOCK=256, num_warps=4,
num_stages=1)
del buf5
del buf6
return buf7, primals_9, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0
), buf2, buf4, primals_7
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class PointerAttentionNew(torch.nn.Module):
"""
attention mechanism in pointer network
Args:
- input_size: The number of features in Hr
- hidden_size: The number of features in the hidden layer
Inputs:
Hr(context_len, batch, hidden_size * num_directions): question-aware context representation
Hk_last(batch, hidden_size): the last hidden output of previous time
Outputs:
beta(batch, context_len): question-aware context representation
"""
def __init__(self, input_size, hidden_size):
super(PointerAttentionNew, self).__init__()
self.linear_wr = torch.nn.Linear(input_size, hidden_size)
self.linear_wa = torch.nn.Linear(hidden_size, hidden_size)
self.linear_wf = torch.nn.Linear(hidden_size, 1)
def forward(self, input_0, input_1, input_2):
primals_1 = self.linear_wr.weight
primals_2 = self.linear_wr.bias
primals_4 = self.linear_wa.weight
primals_5 = self.linear_wa.bias
primals_7 = self.linear_wf.weight
primals_8 = self.linear_wf.bias
primals_3 = input_0
primals_6 = input_1
primals_9 = input_2
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9])
return output[0]
|
jamaalhay/Final_Proj
|
PointerAttention
| false
| 15,665
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
SelfGated
|
import torch
import torch.utils.data
import torch.nn.functional as F
class SelfGated(torch.nn.Module):
"""
Self-Gated layer. math: \\sigmoid(W*x) * x
"""
def __init__(self, input_size):
super(SelfGated, self).__init__()
self.linear_g = torch.nn.Linear(input_size, input_size)
def forward(self, x):
x_l = self.linear_g(x)
x_gt = F.sigmoid(x_l)
x = x * x_gt
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_mul_sigmoid_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask)
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x0, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 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_mul_sigmoid_0[grid(256)](primals_3, buf0, buf1,
256, XBLOCK=128, num_warps=4, num_stages=1)
return buf1, primals_3, buf0
class SelfGatedNew(torch.nn.Module):
"""
Self-Gated layer. math: \\sigmoid(W*x) * x
"""
def __init__(self, input_size):
super(SelfGatedNew, self).__init__()
self.linear_g = torch.nn.Linear(input_size, input_size)
def forward(self, input_0):
primals_1 = self.linear_g.weight
primals_2 = self.linear_g.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
jamaalhay/Final_Proj
|
SelfGated
| false
| 15,666
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
SFU
|
import torch
import torch.utils.data
import torch.nn.functional as F
class SFU(torch.nn.Module):
"""
only two input, one input vector and one fusion vector
Args:
- input_size:
- fusions_size:
Inputs:
- input: (seq_len, batch, input_size)
- fusions: (seq_len, batch, fusions_size)
Outputs:
- output: (seq_len, batch, input_size)
"""
def __init__(self, input_size, fusions_size):
super(SFU, self).__init__()
self.linear_r = torch.nn.Linear(input_size + fusions_size, input_size)
self.linear_g = torch.nn.Linear(input_size + fusions_size, input_size)
def forward(self, input, fusions):
m = torch.cat((input, fusions), dim=-1)
r = F.tanh(self.linear_r(m))
g = F.sigmoid(self.linear_g(m))
o = g * r + (1 - g) * input
return o
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'fusions_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.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_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_add_mul_rsub_sigmoid_tanh_1(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp2 = tl.load(in_ptr1 + x0, xmask)
tmp7 = tl.load(in_ptr2 + x0, xmask)
tmp1 = tl.sigmoid(tmp0)
tmp3 = libdevice.tanh(tmp2)
tmp4 = tmp1 * tmp3
tmp5 = 1.0
tmp6 = tmp5 - tmp1
tmp8 = tmp6 * tmp7
tmp9 = tmp4 + tmp8
tl.store(out_ptr0 + x0, tmp9, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4, 8), (8, 1))
assert_size_stride(primals_4, (4,), (1,))
assert_size_stride(primals_5, (4, 8), (8, 1))
assert_size_stride(primals_6, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 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_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((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_6, reinterpret_tensor(buf0, (64, 8), (
8, 1), 0), reinterpret_tensor(primals_5, (8, 4), (1, 8), 0),
alpha=1, beta=1, out=buf2)
del primals_5
del primals_6
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_mul_rsub_sigmoid_tanh_1[grid(256)](buf2, buf1,
primals_1, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1)
return buf3, primals_1, reinterpret_tensor(buf0, (64, 8), (8, 1), 0
), buf1, buf2
class SFUNew(torch.nn.Module):
"""
only two input, one input vector and one fusion vector
Args:
- input_size:
- fusions_size:
Inputs:
- input: (seq_len, batch, input_size)
- fusions: (seq_len, batch, fusions_size)
Outputs:
- output: (seq_len, batch, input_size)
"""
def __init__(self, input_size, fusions_size):
super(SFUNew, self).__init__()
self.linear_r = torch.nn.Linear(input_size + fusions_size, input_size)
self.linear_g = torch.nn.Linear(input_size + fusions_size, input_size)
def forward(self, input_0, input_1):
primals_3 = self.linear_r.weight
primals_4 = self.linear_r.bias
primals_5 = self.linear_g.weight
primals_6 = self.linear_g.bias
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0]
|
jamaalhay/Final_Proj
|
SFU
| false
| 15,667
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
AttentionPooling
|
import torch
import torch.utils.data
import torch.nn.functional as F
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class AttentionPooling(torch.nn.Module):
"""
Attention-Pooling for pointer net init hidden state generate.
Equal to Self-Attention + MLP
Modified from r-net.
Args:
input_size: The number of expected features in the input uq
output_size: The number of expected features in the output rq_o
Inputs: input, mask
- **input** (seq_len, batch, input_size): tensor containing the features
of the input sequence.
- **mask** (batch, seq_len): tensor show whether a padding index for each element in the batch.
Outputs: output
- **output** (batch, output_size): tensor containing the output features
"""
def __init__(self, input_size, output_size):
super(AttentionPooling, self).__init__()
self.linear_u = torch.nn.Linear(input_size, output_size)
self.linear_t = torch.nn.Linear(output_size, 1)
self.linear_o = torch.nn.Linear(input_size, output_size)
def forward(self, uq, mask):
q_tanh = F.tanh(self.linear_u(uq))
q_s = self.linear_t(q_tanh).squeeze(2).transpose(0, 1)
alpha = masked_softmax(q_s, mask, dim=1)
rq = torch.bmm(alpha.unsqueeze(1), uq.transpose(0, 1)).squeeze(1)
rq_o = F.tanh(self.linear_o(rq))
return rq_o
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'output_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
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_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(in_out_ptr0 + x2, tmp3, xmask)
@triton.jit
def triton_poi_fused_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (4 + x0), xmask)
tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (8 + x0), xmask)
tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (12 + x0), xmask)
tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 * tmp1
tmp5 = tmp3 * tmp4
tmp6 = triton_helpers.maximum(tmp2, tmp5)
tmp9 = tmp7 * tmp8
tmp10 = triton_helpers.maximum(tmp6, tmp9)
tmp13 = tmp11 * tmp12
tmp14 = triton_helpers.maximum(tmp10, tmp13)
tmp15 = tmp2 - tmp14
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp1
tmp18 = tmp5 - tmp14
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp19 * tmp4
tmp21 = tmp17 + tmp20
tmp22 = tmp9 - tmp14
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp23 * tmp8
tmp25 = tmp21 + tmp24
tmp26 = tmp13 - tmp14
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp27 * tmp12
tmp29 = tmp25 + tmp28
tl.store(out_ptr0 + x0, tmp14, xmask)
tl.store(out_ptr1 + x0, tmp29, xmask)
@triton.jit
def triton_poi_fused_add_div_exp_max_mul_sub_2(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, 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)
tmp1 = tl.load(in_ptr1 + (x1 + 4 * y0), xmask & ymask)
tmp3 = tl.load(in_ptr2 + y0, ymask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr3 + y0, ymask, eviction_policy='evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp5 * tmp1
tmp8 = 1e-06
tmp9 = tmp7 + tmp8
tmp10 = tmp6 / tmp9
tl.store(out_ptr0 + (x1 + 4 * y0), tmp10, xmask & ymask)
@triton.jit
def triton_poi_fused_tanh_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(in_out_ptr0 + x2, tmp3, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_4, (1, 4), (4, 1))
assert_size_stride(primals_5, (1,), (1,))
assert_size_stride(primals_6, (4, 4), (4, 1))
assert_size_stride(primals_7, (4, 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 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_tanh_0[grid(64)](buf1, primals_2, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_2
buf3 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (16, 4), (
4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0),
alpha=1, beta=1, out=buf3)
del primals_5
buf4 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
triton_poi_fused_exp_max_mul_sub_sum_1[grid(4)](buf3, primals_6,
buf4, buf5, 4, XBLOCK=4, num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_div_exp_max_mul_sub_2[grid(4, 4)](buf3,
primals_6, buf4, buf5, buf6, 4, 4, XBLOCK=4, YBLOCK=4,
num_warps=1, num_stages=1)
del buf4
del buf5
buf7 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf6, (4, 1, 4), (4, 0, 1), 0
), reinterpret_tensor(primals_3, (4, 4, 4), (4, 16, 1), 0), out
=buf7)
buf8 = buf6
del buf6
extern_kernels.mm(reinterpret_tensor(buf7, (4, 4), (4, 1), 0),
reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf8)
buf9 = buf8
del buf8
triton_poi_fused_tanh_3[grid(16)](buf9, primals_8, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_8
return buf9, primals_3, primals_6, buf1, buf3, reinterpret_tensor(buf7,
(4, 4), (4, 1), 0), buf9, primals_7, primals_4
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class AttentionPoolingNew(torch.nn.Module):
"""
Attention-Pooling for pointer net init hidden state generate.
Equal to Self-Attention + MLP
Modified from r-net.
Args:
input_size: The number of expected features in the input uq
output_size: The number of expected features in the output rq_o
Inputs: input, mask
- **input** (seq_len, batch, input_size): tensor containing the features
of the input sequence.
- **mask** (batch, seq_len): tensor show whether a padding index for each element in the batch.
Outputs: output
- **output** (batch, output_size): tensor containing the output features
"""
def __init__(self, input_size, output_size):
super(AttentionPoolingNew, self).__init__()
self.linear_u = torch.nn.Linear(input_size, output_size)
self.linear_t = torch.nn.Linear(output_size, 1)
self.linear_o = torch.nn.Linear(input_size, output_size)
def forward(self, input_0, input_1):
primals_1 = self.linear_u.weight
primals_2 = self.linear_u.bias
primals_4 = self.linear_t.weight
primals_5 = self.linear_t.bias
primals_6 = self.linear_o.weight
primals_8 = self.linear_o.bias
primals_3 = input_0
primals_7 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8])
return output[0]
|
jamaalhay/Final_Proj
|
AttentionPooling
| false
| 15,668
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
SegmentationHead
|
import torch
import torch.nn as nn
import torch.utils.data.dataloader
class SegmentationHead(nn.Module):
def __init__(self, descriptor_dimension, num_classes, **kwargs):
super().__init__()
self.descriptor_dimension = descriptor_dimension
self.classifier = nn.Conv2d(in_channels=descriptor_dimension,
out_channels=num_classes, kernel_size=1, bias=True)
def forward(self, input):
return self.classifier(input[0].detach())
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'descriptor_dimension': 4, 'num_classes': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
import torch.utils.data.dataloader
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 16
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = 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,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
4, 4, 4), (64, 16, 4, 1), 0), 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, (1, 4, 4, 4), (64, 16, 4, 1))
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_convolution_0[grid(64)](buf1, primals_3, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0
), primals_2, reinterpret_tensor(primals_1, (1, 4, 4, 4), (64, 16,
4, 1), 0)
class SegmentationHeadNew(nn.Module):
def __init__(self, descriptor_dimension, num_classes, **kwargs):
super().__init__()
self.descriptor_dimension = descriptor_dimension
self.classifier = nn.Conv2d(in_channels=descriptor_dimension,
out_channels=num_classes, kernel_size=1, bias=True)
def forward(self, input_0):
primals_2 = self.classifier.weight
primals_3 = self.classifier.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
jamt9000/DVE
|
SegmentationHead
| false
| 15,669
|
[
"MIT"
] | 72
|
208514419dd1eb0d27ce60876ca836d1ab8c4f4a
|
https://github.com/jamt9000/DVE/tree/208514419dd1eb0d27ce60876ca836d1ab8c4f4a
|
MedianPool2d
|
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.utils import _pair
from torch.nn.modules.utils import _quadruple
import torch.optim
class MedianPool2d(nn.Module):
""" Median pool (usable as median filter when stride=1) module.
Args:
kernel_size: size of pooling kernel, int or 2-tuple
stride: pool stride, int or 2-tuple
padding: pool padding, int or 4-tuple (l, r, t, b) as in pytorch F.pad
same: override padding and enforce same padding, boolean
"""
def __init__(self, kernel_size=3, stride=1, padding=0, same=False):
super(MedianPool2d, self).__init__()
self.k = _pair(kernel_size)
self.stride = _pair(stride)
self.padding = _quadruple(padding)
self.same = same
def _padding(self, x):
if self.same:
ih, iw = x.size()[2:]
if ih % self.stride[0] == 0:
ph = max(self.k[0] - self.stride[0], 0)
else:
ph = max(self.k[0] - ih % self.stride[0], 0)
if iw % self.stride[1] == 0:
pw = max(self.k[1] - self.stride[1], 0)
else:
pw = max(self.k[1] - iw % self.stride[1], 0)
pl = pw // 2
pr = pw - pl
pt = ph // 2
pb = ph - pt
padding = pl, pr, pt, pb
else:
padding = self.padding
return padding
def forward(self, x):
x = F.pad(x, self._padding(x), mode='reflect')
x = x.unfold(2, self.k[0], self.stride[0]).unfold(3, self.k[1],
self.stride[1])
x = x.contiguous().view(x.size()[:4] + (-1,)).median(dim=-1)[0]
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
from torch.nn.modules.utils import _pair
from torch.nn.modules.utils import _quadruple
import torch.optim
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 64
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
x3 = xindex % 3
x4 = xindex // 3
y0 = yindex % 2
y1 = yindex // 2 % 2
y2 = yindex // 4
x6 = xindex
y5 = yindex
tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + x3 + y0) + -4 *
tl_math.abs(-3 + x4 + y1) + 16 * y2), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x6 + 9 * y5), tmp0, xmask & ymask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 2, 2, 3, 3), (144, 36, 18, 9, 3, 1
), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(64, 9)](arg0_1, buf0, 64, 9, XBLOCK=
16, YBLOCK=64, num_warps=4, num_stages=1)
del arg0_1
buf1 = torch.ops.aten.median.dim(reinterpret_tensor(buf0, (4, 4, 2,
2, 9), (144, 36, 18, 9, 1), 0), -1)
del buf0
buf2 = buf1[0]
del buf1
return buf2,
class MedianPool2dNew(nn.Module):
""" Median pool (usable as median filter when stride=1) module.
Args:
kernel_size: size of pooling kernel, int or 2-tuple
stride: pool stride, int or 2-tuple
padding: pool padding, int or 4-tuple (l, r, t, b) as in pytorch F.pad
same: override padding and enforce same padding, boolean
"""
def __init__(self, kernel_size=3, stride=1, padding=0, same=False):
super(MedianPool2dNew, self).__init__()
self.k = _pair(kernel_size)
self.stride = _pair(stride)
self.padding = _quadruple(padding)
self.same = same
def _padding(self, x):
if self.same:
ih, iw = x.size()[2:]
if ih % self.stride[0] == 0:
ph = max(self.k[0] - self.stride[0], 0)
else:
ph = max(self.k[0] - ih % self.stride[0], 0)
if iw % self.stride[1] == 0:
pw = max(self.k[1] - self.stride[1], 0)
else:
pw = max(self.k[1] - iw % self.stride[1], 0)
pl = pw // 2
pr = pw - pl
pt = ph // 2
pb = ph - pt
padding = pl, pr, pt, pb
else:
padding = self.padding
return padding
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
jammer345/3DGNN_pytorch
|
MedianPool2d
| false
| 15,670
|
[
"MIT"
] | 231
|
34a5b3890f23e03fa6cc316c79498eeaea635664
|
https://github.com/jammer345/3DGNN_pytorch/tree/34a5b3890f23e03fa6cc316c79498eeaea635664
|
ForwardNet
|
import torch
import torch.utils.data
import torch.nn.functional as F
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class ForwardNet(torch.nn.Module):
"""
one hidden layer and one softmax layer.
Args:
- input_size:
- hidden_size:
- output_size:
- dropout_p:
Inputs:
- x: (seq_len, batch, input_size)
- x_mask: (batch, seq_len)
Outputs:
- beta: (batch, seq_len)
"""
def __init__(self, input_size, hidden_size, dropout_p):
super(ForwardNet, self).__init__()
self.linear_h = torch.nn.Linear(input_size, hidden_size)
self.linear_o = torch.nn.Linear(hidden_size, 1)
self.dropout = torch.nn.Dropout(p=dropout_p)
def forward(self, x, x_mask):
h = F.relu(self.linear_h(x))
h = self.dropout(h)
o = self.linear_o(h)
o = o.squeeze(2).transpose(0, 1)
beta = masked_softmax(o, x_mask, dim=1)
return beta
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'hidden_size': 4, 'dropout_p': 0.5}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex // 4
x2 = xindex // 16
x4 = xindex % 16
x5 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x4 + 64 * x2), xmask)
tmp3 = tl.load(in_ptr0 + (16 + x3), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (16 + x4 + 64 * x2), xmask)
tmp7 = tl.load(in_ptr0 + (32 + x3), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (32 + x4 + 64 * x2), xmask)
tmp11 = tl.load(in_ptr0 + (48 + x3), xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr1 + (48 + x4 + 64 * x2), xmask)
tmp2 = tmp0 * tmp1
tmp5 = tmp3 * tmp4
tmp6 = triton_helpers.maximum(tmp2, tmp5)
tmp9 = tmp7 * tmp8
tmp10 = triton_helpers.maximum(tmp6, tmp9)
tmp13 = tmp11 * tmp12
tmp14 = triton_helpers.maximum(tmp10, tmp13)
tmp15 = tmp2 - tmp14
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp1
tmp18 = tmp5 - tmp14
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp19 * tmp4
tmp21 = tmp17 + tmp20
tmp22 = tmp9 - tmp14
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp23 * tmp8
tmp25 = tmp21 + tmp24
tmp26 = tmp13 - tmp14
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp27 * tmp12
tmp29 = tmp25 + tmp28
tl.store(out_ptr0 + x5, tmp14, xmask)
tl.store(out_ptr1 + x5, tmp29, xmask)
@triton.jit
def triton_poi_fused_add_div_exp_max_mul_sub_2(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4 % 4
x2 = xindex // 16 % 4
x3 = xindex // 64
x4 = xindex
x5 = xindex % 16
tmp0 = tl.load(in_ptr0 + (x1 + 4 * x3 + 16 * x2), xmask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x4, xmask)
tmp3 = tl.load(in_ptr2 + (x5 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr3 + (x5 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp5 * tmp1
tmp8 = 1e-06
tmp9 = tmp7 + tmp8
tmp10 = tmp6 / tmp9
tl.store(out_ptr0 + (x5 + 16 * x3 + 64 * x2), tmp10, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (1, 4), (4, 1))
assert_size_stride(primals_5, (1,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1,
primals_2, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf3 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), (
4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0),
alpha=1, beta=1, out=buf3)
del primals_5
buf4 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
buf5 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
triton_poi_fused_exp_max_mul_sub_sum_1[grid(64)](buf3, primals_6,
buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((4, 4, 4, 4), (16, 64, 4, 1), torch.float32)
triton_poi_fused_add_div_exp_max_mul_sub_2[grid(256)](buf3,
primals_6, buf4, buf5, buf6, 256, XBLOCK=256, num_warps=4,
num_stages=1)
del buf4
del buf5
return buf6, primals_6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf3, primals_4, buf7
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class ForwardNetNew(torch.nn.Module):
"""
one hidden layer and one softmax layer.
Args:
- input_size:
- hidden_size:
- output_size:
- dropout_p:
Inputs:
- x: (seq_len, batch, input_size)
- x_mask: (batch, seq_len)
Outputs:
- beta: (batch, seq_len)
"""
def __init__(self, input_size, hidden_size, dropout_p):
super(ForwardNetNew, self).__init__()
self.linear_h = torch.nn.Linear(input_size, hidden_size)
self.linear_o = torch.nn.Linear(hidden_size, 1)
self.dropout = torch.nn.Dropout(p=dropout_p)
def forward(self, input_0, input_1):
primals_1 = self.linear_h.weight
primals_2 = self.linear_h.bias
primals_4 = self.linear_o.weight
primals_5 = self.linear_o.bias
primals_3 = input_0
primals_6 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0]
|
jamaalhay/Final_Proj
|
ForwardNet
| false
| 15,671
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
SelfAttentionGated
|
import torch
import torch.utils.data
import torch.nn.functional as F
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class SelfAttentionGated(torch.nn.Module):
"""
Self-Attention Gated layer, it`s not weighted sum in the last, but just weighted
math: \\softmax(W* anh(W*x)) * x
Args:
input_size: The number of expected features in the input x
Inputs: input, mask
- **input** (seq_len, batch, input_size): tensor containing the features
of the input sequence.
- **mask** (batch, seq_len): tensor show whether a padding index for each element in the batch.
Outputs: output
- **output** (seq_len, batch, input_size): gated output tensor
"""
def __init__(self, input_size):
super(SelfAttentionGated, self).__init__()
self.linear_g = torch.nn.Linear(input_size, input_size)
self.linear_t = torch.nn.Linear(input_size, 1)
def forward(self, x, x_mask):
g_tanh = F.tanh(self.linear_g(x))
gt = self.linear_t.forward(g_tanh).squeeze(2).transpose(0, 1)
gt_prop = masked_softmax(gt, x_mask, dim=1)
gt_prop = gt_prop.transpose(0, 1).unsqueeze(2)
x_gt = x * gt_prop
return x_gt
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import 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_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(in_out_ptr0 + x2, tmp3, xmask)
@triton.jit
def triton_poi_fused_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex // 4
x2 = xindex // 16
x4 = xindex % 16
x5 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x4 + 64 * x2), xmask)
tmp3 = tl.load(in_ptr0 + (16 + x3), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (16 + x4 + 64 * x2), xmask)
tmp7 = tl.load(in_ptr0 + (32 + x3), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (32 + x4 + 64 * x2), xmask)
tmp11 = tl.load(in_ptr0 + (48 + x3), xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr1 + (48 + x4 + 64 * x2), xmask)
tmp2 = tmp0 * tmp1
tmp5 = tmp3 * tmp4
tmp6 = triton_helpers.maximum(tmp2, tmp5)
tmp9 = tmp7 * tmp8
tmp10 = triton_helpers.maximum(tmp6, tmp9)
tmp13 = tmp11 * tmp12
tmp14 = triton_helpers.maximum(tmp10, tmp13)
tmp15 = tmp2 - tmp14
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp1
tmp18 = tmp5 - tmp14
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp19 * tmp4
tmp21 = tmp17 + tmp20
tmp22 = tmp9 - tmp14
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp23 * tmp8
tmp25 = tmp21 + tmp24
tmp26 = tmp13 - tmp14
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp27 * tmp12
tmp29 = tmp25 + tmp28
tl.store(out_ptr0 + x5, tmp14, xmask)
tl.store(out_ptr1 + x5, tmp29, xmask)
@triton.jit
def triton_poi_fused_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x5 = xindex % 256
x1 = xindex // 4 % 4
x6 = xindex // 64
x3 = xindex // 64 % 4
x4 = xindex // 256
x7 = xindex % 16
x8 = xindex
tmp0 = tl.load(in_ptr0 + x5, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x1 + 4 * x6), xmask, eviction_policy='evict_last'
)
tmp2 = tl.load(in_ptr2 + (x7 + 16 * x4 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp4 = tl.load(in_ptr3 + (x7 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr4 + (x7 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 * tmp2
tmp5 = tmp3 - tmp4
tmp6 = tl_math.exp(tmp5)
tmp7 = tmp6 * tmp2
tmp9 = 1e-06
tmp10 = tmp8 + tmp9
tmp11 = tmp7 / tmp10
tmp12 = tmp0 * tmp11
tl.store(out_ptr0 + x8, tmp12, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (1, 4), (4, 1))
assert_size_stride(primals_5, (1,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_2
buf3 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), (
4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0),
alpha=1, beta=1, out=buf3)
del primals_5
buf4 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
buf5 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
triton_poi_fused_exp_max_mul_sub_sum_1[grid(64)](buf3, primals_6,
buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_mul_2[grid(1024)](primals_3, buf3, primals_6, buf4,
buf5, buf6, 1024, XBLOCK=128, num_warps=4, num_stages=1)
del buf4
del buf5
return buf6, primals_3, primals_6, buf1, buf3, primals_4
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class SelfAttentionGatedNew(torch.nn.Module):
"""
Self-Attention Gated layer, it`s not weighted sum in the last, but just weighted
math: \\softmax(W* anh(W*x)) * x
Args:
input_size: The number of expected features in the input x
Inputs: input, mask
- **input** (seq_len, batch, input_size): tensor containing the features
of the input sequence.
- **mask** (batch, seq_len): tensor show whether a padding index for each element in the batch.
Outputs: output
- **output** (seq_len, batch, input_size): gated output tensor
"""
def __init__(self, input_size):
super(SelfAttentionGatedNew, self).__init__()
self.linear_g = torch.nn.Linear(input_size, input_size)
self.linear_t = torch.nn.Linear(input_size, 1)
def forward(self, input_0, input_1):
primals_1 = self.linear_g.weight
primals_2 = self.linear_g.bias
primals_4 = self.linear_t.weight
primals_5 = self.linear_t.bias
primals_3 = input_0
primals_6 = input_1
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0]
|
jamaalhay/Final_Proj
|
SelfAttentionGated
| false
| 15,672
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
MatchRNNAttention
|
import torch
import torch.utils.data
import torch.nn.functional as F
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class MatchRNNAttention(torch.nn.Module):
"""
attention mechanism in match-rnn
Args:
- input_size: The number of expected features in the input Hp and Hq
- hidden_size: The number of features in the hidden state Hr
Inputs:
Hpi(batch, input_size): a context word encoded
Hq(question_len, batch, input_size): whole question encoded
Hr_last(batch, hidden_size): last lstm hidden output
Outputs:
alpha(batch, question_len): attention vector
"""
def __init__(self, hp_input_size, hq_input_size, hidden_size):
super(MatchRNNAttention, self).__init__()
self.linear_wq = torch.nn.Linear(hq_input_size, hidden_size)
self.linear_wp = torch.nn.Linear(hp_input_size, hidden_size)
self.linear_wr = torch.nn.Linear(hidden_size, hidden_size)
self.linear_wg = torch.nn.Linear(hidden_size, 1)
def forward(self, Hpi, Hq, Hr_last, Hq_mask):
wq_hq = self.linear_wq(Hq)
wp_hp = self.linear_wp(Hpi).unsqueeze(0)
wr_hr = self.linear_wr(Hr_last).unsqueeze(0)
G = F.tanh(wq_hq + wp_hp + wr_hr)
wg_g = self.linear_wg(G).squeeze(2).transpose(0, 1)
alpha = masked_softmax(wg_g, m=Hq_mask, dim=1)
return alpha
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'hp_input_size': 4, 'hq_input_size': 4, 'hidden_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
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_add_tanh_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr3 + x2, xmask)
tmp8 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 + tmp8
tmp10 = tmp6 + tmp9
tmp11 = libdevice.tanh(tmp10)
tl.store(in_out_ptr0 + x2, tmp11, xmask)
@triton.jit
def triton_poi_fused_add_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex // 4
x4 = xindex % 64
x5 = xindex
tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (64 + x4), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr1 + (128 + x4), xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr1 + (192 + x4), xmask, eviction_policy='evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp0 * tmp3
tmp5 = triton_helpers.maximum(tmp2, tmp4)
tmp7 = tmp0 * tmp6
tmp8 = triton_helpers.maximum(tmp5, tmp7)
tmp10 = tmp0 * tmp9
tmp11 = triton_helpers.maximum(tmp8, tmp10)
tmp12 = tmp2 - tmp11
tmp13 = tl_math.exp(tmp12)
tmp14 = tmp13 * tmp1
tmp15 = tmp4 - tmp11
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp16 * tmp3
tmp18 = tmp14 + tmp17
tmp19 = tmp7 - tmp11
tmp20 = tl_math.exp(tmp19)
tmp21 = tmp20 * tmp6
tmp22 = tmp18 + tmp21
tmp23 = tmp10 - tmp11
tmp24 = tl_math.exp(tmp23)
tmp25 = tmp24 * tmp9
tmp26 = tmp22 + tmp25
tmp27 = 1e-06
tmp28 = tmp26 + tmp27
tl.store(out_ptr0 + x5, tmp11, xmask)
tl.store(out_ptr1 + x5, tmp28, xmask)
@triton.jit
def triton_poi_fused_add_div_exp_max_mul_sub_sum_2(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4 % 16
x3 = xindex // 256
x4 = xindex % 256
x5 = xindex % 64
x6 = xindex
tmp0 = tl.load(in_ptr0 + (x1 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + (x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr3 + (x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp5 * tmp1
tmp8 = tmp6 / tmp7
tl.store(out_ptr0 + x6, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12
) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_7, (4, 4), (4, 1))
assert_size_stride(primals_8, (4,), (1,))
assert_size_stride(primals_9, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_10, (1, 4), (4, 1))
assert_size_stride(primals_11, (1,), (1,))
assert_size_stride(primals_12, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1)
del primals_4
buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_9, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2)
del primals_7
buf3 = reinterpret_tensor(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
)
del buf0
get_raw_stream(0)
triton_poi_fused_add_tanh_0[grid(256)](buf3, primals_2, buf1,
primals_5, buf2, primals_8, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del primals_2
del primals_5
del primals_8
buf5 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_11, reinterpret_tensor(buf3, (64, 4),
(4, 1), 0), reinterpret_tensor(primals_10, (4, 1), (1, 4), 0),
alpha=1, beta=1, out=buf5)
del primals_11
buf6 = reinterpret_tensor(buf2, (4, 1, 4, 4, 4), (64, 256, 16, 4, 1), 0
)
del buf2
buf7 = reinterpret_tensor(buf1, (4, 1, 4, 4, 4), (64, 256, 16, 4, 1), 0
)
del buf1
triton_poi_fused_add_exp_max_mul_sub_sum_1[grid(256)](buf5,
primals_12, buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1)
buf8 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_add_div_exp_max_mul_sub_sum_2[grid(1024)](buf5,
primals_12, buf6, buf7, buf8, 1024, XBLOCK=256, num_warps=4,
num_stages=1)
del buf6
del buf7
return buf8, primals_12, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_9, (64, 4), (4, 1), 0
), buf3, buf5, primals_10
def masked_softmax(x, m=None, dim=-1):
"""
Softmax with mask
:param x:
:param m:
:param dim:
:return:
"""
if m is not None:
m = m.float()
x = x * m
e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
if m is not None:
e_x = e_x * m
softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
return softmax
class MatchRNNAttentionNew(torch.nn.Module):
"""
attention mechanism in match-rnn
Args:
- input_size: The number of expected features in the input Hp and Hq
- hidden_size: The number of features in the hidden state Hr
Inputs:
Hpi(batch, input_size): a context word encoded
Hq(question_len, batch, input_size): whole question encoded
Hr_last(batch, hidden_size): last lstm hidden output
Outputs:
alpha(batch, question_len): attention vector
"""
def __init__(self, hp_input_size, hq_input_size, hidden_size):
super(MatchRNNAttentionNew, self).__init__()
self.linear_wq = torch.nn.Linear(hq_input_size, hidden_size)
self.linear_wp = torch.nn.Linear(hp_input_size, hidden_size)
self.linear_wr = torch.nn.Linear(hidden_size, hidden_size)
self.linear_wg = torch.nn.Linear(hidden_size, 1)
def forward(self, input_0, input_1, input_2, input_3):
primals_1 = self.linear_wq.weight
primals_2 = self.linear_wq.bias
primals_4 = self.linear_wp.weight
primals_5 = self.linear_wp.bias
primals_7 = self.linear_wr.weight
primals_8 = self.linear_wr.bias
primals_10 = self.linear_wg.weight
primals_11 = self.linear_wg.bias
primals_3 = input_0
primals_6 = input_1
primals_9 = input_2
primals_12 = input_3
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12])
return output[0]
|
jamaalhay/Final_Proj
|
MatchRNNAttention
| false
| 15,673
|
[
"MIT"
] | 104
|
3f524a90fee5a3cb21466ab76f630d060792045d
|
https://github.com/jamaalhay/Final_Proj/tree/3f524a90fee5a3cb21466ab76f630d060792045d
|
Classifier
|
import torch
import torch.nn as nn
import torch.utils.data
import torch.onnx.operators
import torch.optim
import torch.optim.lr_scheduler
import torch.distributed
class Classifier(nn.Module):
def __init__(self, hidden_size):
super(Classifier, self).__init__()
self.linear1 = nn.Linear(hidden_size, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x, mask_cls):
h = self.linear1(x).squeeze(-1)
sent_scores = self.sigmoid(h) * mask_cls.float()
return sent_scores
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'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
import torch.nn as nn
import torch.utils.data
import torch.onnx.operators
import torch.optim
import torch.optim.lr_scheduler
import torch.distributed
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_sigmoid_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 64
x2 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr1 + x2, xmask)
tmp1 = tl.sigmoid(tmp0)
tmp3 = tmp1 * tmp2
tl.store(out_ptr0 + x2, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = 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, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64,
4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0
), alpha=1, beta=1, out=buf1)
del primals_1
del primals_2
buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mul_sigmoid_0[grid(256)](buf1, primals_4, buf2,
256, XBLOCK=128, num_warps=4, num_stages=1)
return buf2, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), buf1
class ClassifierNew(nn.Module):
def __init__(self, hidden_size):
super(ClassifierNew, self).__init__()
self.linear1 = nn.Linear(hidden_size, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, input_0, input_1):
primals_1 = self.linear1.weight
primals_2 = self.linear1.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
jantrienes/guided_summarization
|
Classifier
| false
| 15,674
|
[
"MIT"
] | 65
|
547beee09ba6e9158f2681279131f9b5d7ed31ab
|
https://github.com/jantrienes/guided_summarization/tree/547beee09ba6e9158f2681279131f9b5d7ed31ab
|
TactileWeightModel
|
import torch
import torch.utils.data
import torch.nn as nn
from typing import Optional
import torch.linalg
class TactileWeightModel(nn.Module):
def __init__(self, device: 'torch.device', dim: 'int'=3, wt_init:
'Optional[torch.Tensor]'=None):
super().__init__()
wt_init_ = torch.rand(1, dim)
if wt_init is not None:
wt_init_ = wt_init
self.param = nn.Parameter(wt_init_)
self
def forward(self):
return self.param.clone()
def get_inputs():
return []
def get_init_inputs():
return [[], {'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
import torch.utils.data
import torch.nn as nn
from typing import Optional
import torch.linalg
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 3
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):
primals_1, = args
args.clear()
assert_size_stride(primals_1, (1, 3), (3, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((1, 3), (3, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(3)](primals_1, buf0, 3, XBLOCK=4,
num_warps=1, num_stages=1)
del primals_1
return buf0,
class TactileWeightModelNew(nn.Module):
def __init__(self, device: 'torch.device', dim: 'int'=3, wt_init:
'Optional[torch.Tensor]'=None):
super().__init__()
wt_init_ = torch.rand(1, dim)
if wt_init is not None:
wt_init_ = wt_init
self.param = nn.Parameter(wt_init_)
self
def forward(self):
primals_1 = self.param
output = call([primals_1])
return output[0]
|
jeffin07/theseus
|
TactileWeightModel
| false
| 15,676
|
[
"MIT"
] | 236
|
3498bbddf9cca740c2703d0c1aa3a78a7264cb15
|
https://github.com/jeffin07/theseus/tree/3498bbddf9cca740c2703d0c1aa3a78a7264cb15
|
RobertaClassificationHead
|
from _paritybench_helpers import _mock_config
import torch
from torch import nn
class RobertaClassificationHead(nn.Module):
"""Head for sentence-level classification tasks."""
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, 128)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.out_proj = nn.Linear(128, config.num_labels)
def forward(self, features, **kwargs):
x = features[:, 0, :]
x = self.dropout(x)
x = self.dense(x)
x = torch.tanh(x)
x = self.dropout(x)
x = self.out_proj(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'config': _mock_config(hidden_size=4, hidden_dropout_prob=
0.5, num_labels=4)}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tl.store(out_ptr0 + x2, tmp0, xmask)
@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 % 128
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 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (128, 4), (4, 1))
assert_size_stride(primals_3, (128,), (1,))
assert_size_stride(primals_4, (4, 128), (128, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(64)](primals_1, buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_1
buf1 = empty_strided_cuda((16, 128), (128, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 128), (1, 4), 0), out=buf1)
del primals_2
buf2 = reinterpret_tensor(buf1, (4, 4, 128), (512, 128, 1), 0)
del buf1
triton_poi_fused_tanh_1[grid(2048)](buf2, primals_3, 2048, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (16, 128),
(128, 1), 0), reinterpret_tensor(primals_4, (128, 4), (1, 128),
0), alpha=1, beta=1, out=buf3)
del primals_5
return reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0
), reinterpret_tensor(buf0, (16, 4), (4, 1), 0), buf2, primals_4
class RobertaClassificationHeadNew(nn.Module):
"""Head for sentence-level classification tasks."""
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, 128)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.out_proj = nn.Linear(128, config.num_labels)
def forward(self, input_0):
primals_2 = self.dense.weight
primals_3 = self.dense.bias
primals_4 = self.out_proj.weight
primals_5 = self.out_proj.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
HebatallaTarek/Empathy-Mental-Health
|
RobertaClassificationHead
| false
| 15,677
|
[
"BSD-3-Clause"
] | 66
|
16e2a5f93aabd22803bb39805f8e76c8bea0ccf2
|
https://github.com/HebatallaTarek/Empathy-Mental-Health/tree/16e2a5f93aabd22803bb39805f8e76c8bea0ccf2
|
UpBlock
|
import torch
import torch.nn as nn
from torch.nn import functional as F
class UpBlock(nn.Module):
"""Upsample block for DRRG and TextSnake."""
def __init__(self, in_channels, out_channels):
super().__init__()
assert isinstance(in_channels, int)
assert isinstance(out_channels, int)
self.conv1x1 = nn.Conv2d(in_channels, in_channels, kernel_size=1,
stride=1, padding=0)
self.conv3x3 = nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=1, padding=1)
self.deconv = nn.ConvTranspose2d(out_channels, out_channels,
kernel_size=4, stride=2, padding=1)
def forward(self, x):
x = F.relu(self.conv1x1(x))
x = F.relu(self.conv3x3(x))
x = self.deconv(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
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
@triton.jit
def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x3, tmp4, xmask)
@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 64 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 1, 1), (4, 1, 1, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_convolution_relu_0[grid(256)](buf1, primals_2, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf2, (4, 4, 4, 4), (64, 16, 4, 1))
buf3 = buf2
del buf2
triton_poi_fused_convolution_relu_0[grid(256)](buf3, primals_5, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_5
buf4 = extern_kernels.convolution(buf3, primals_6, stride=(2, 2),
padding=(1, 1), dilation=(1, 1), transposed=True,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf4, (4, 4, 8, 8), (256, 64, 8, 1))
buf5 = buf4
del buf4
triton_poi_fused_convolution_1[grid(1024)](buf5, primals_7, 1024,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_7
return buf5, primals_1, primals_3, primals_4, primals_6, buf1, buf3
class UpBlockNew(nn.Module):
"""Upsample block for DRRG and TextSnake."""
def __init__(self, in_channels, out_channels):
super().__init__()
assert isinstance(in_channels, int)
assert isinstance(out_channels, int)
self.conv1x1 = nn.Conv2d(in_channels, in_channels, kernel_size=1,
stride=1, padding=0)
self.conv3x3 = nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=1, padding=1)
self.deconv = nn.ConvTranspose2d(out_channels, out_channels,
kernel_size=4, stride=2, padding=1)
def forward(self, input_0):
primals_1 = self.conv1x1.weight
primals_2 = self.conv1x1.bias
primals_4 = self.conv3x3.weight
primals_5 = self.conv3x3.bias
primals_3 = self.deconv.weight
primals_7 = self.deconv.bias
primals_6 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
jeffreykuang/mmocr-1
|
UpBlock
| false
| 15,678
|
[
"Apache-2.0"
] | 206
|
b17304edeb493b0a4d7224c23d23b952350d0db5
|
https://github.com/jeffreykuang/mmocr-1/tree/b17304edeb493b0a4d7224c23d23b952350d0db5
|
RobustScannerFusionLayer
|
import torch
import torch.nn as nn
class RobustScannerFusionLayer(nn.Module):
def __init__(self, dim_model, dim=-1):
super().__init__()
self.dim_model = dim_model
self.dim = dim
self.linear_layer = nn.Linear(dim_model * 2, dim_model * 2)
self.glu_layer = nn.GLU(dim=dim)
def forward(self, x0, x1):
assert x0.size() == x1.size()
fusion_input = torch.cat([x0, x1], self.dim)
output = self.linear_layer(fusion_input)
output = self.glu_layer(output)
return output
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'dim_model': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 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_glu_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x2, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = 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, (8, 8), (8, 1))
assert_size_stride(primals_4, (8,), (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, 8), (8, 1), torch.float32)
extern_kernels.addmm(primals_4, reinterpret_tensor(buf0, (64, 8), (
8, 1), 0), reinterpret_tensor(primals_3, (8, 8), (1, 8), 0),
alpha=1, beta=1, out=buf1)
del primals_3
del primals_4
buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_glu_1[grid(256)](buf1, buf2, 256, XBLOCK=256,
num_warps=4, num_stages=1)
return buf2, reinterpret_tensor(buf0, (64, 8), (8, 1), 0
), reinterpret_tensor(buf1, (4, 4, 4, 8), (128, 32, 8, 1), 0)
class RobustScannerFusionLayerNew(nn.Module):
def __init__(self, dim_model, dim=-1):
super().__init__()
self.dim_model = dim_model
self.dim = dim
self.linear_layer = nn.Linear(dim_model * 2, dim_model * 2)
self.glu_layer = nn.GLU(dim=dim)
def forward(self, input_0, input_1):
primals_3 = self.linear_layer.weight
primals_4 = self.linear_layer.bias
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
jeffreykuang/mmocr-1
|
RobustScannerFusionLayer
| false
| 15,679
|
[
"Apache-2.0"
] | 206
|
b17304edeb493b0a4d7224c23d23b952350d0db5
|
https://github.com/jeffreykuang/mmocr-1/tree/b17304edeb493b0a4d7224c23d23b952350d0db5
|
injective_pad
|
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
class injective_pad(nn.Module):
def __init__(self, pad_size):
super(injective_pad, self).__init__()
self.pad_size = pad_size
self.pad = nn.ZeroPad2d((0, 0, 0, pad_size))
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.pad_size, :, :]
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'pad_size': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_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 injective_padNew(nn.Module):
def __init__(self, pad_size):
super(injective_padNew, self).__init__()
self.pad_size = pad_size
self.pad = nn.ZeroPad2d((0, 0, 0, pad_size))
def inverse(self, x):
return x[:, :x.size(1) - self.pad_size, :, :]
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
jhjacobsen/pytorch-i-revnet
|
injective_pad
| false
| 15,680
|
[
"MIT"
] | 392
|
307413043e33540cbe9c3746ef420261f8138315
|
https://github.com/jhjacobsen/pytorch-i-revnet/tree/307413043e33540cbe9c3746ef420261f8138315
|
MeanMaxPooling
|
import torch
from torch import nn
class MeanMaxPooling(nn.Module):
def __init__(self):
super(MeanMaxPooling, self).__init__()
def forward(self, doc_state, entity_mapping, entity_lens):
"""
:param doc_state: N x L x d
:param entity_mapping: N x E x L
:param entity_lens: N x E
:return: N x E x 2d
"""
entity_states = entity_mapping.unsqueeze(3) * doc_state.unsqueeze(1)
max_pooled = torch.max(entity_states, dim=2)[0]
mean_pooled = torch.sum(entity_states, dim=2) / entity_lens.unsqueeze(2
)
output = torch.cat([max_pooled, mean_pooled], dim=2)
return output
def get_inputs():
return [torch.rand([4, 4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.
rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_div_max_mul_sum_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex // 16 % 4
x3 = xindex // 64 % 4
x4 = xindex // 256
x5 = xindex % 64
x6 = xindex % 16
x7 = xindex // 64
tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x5 + 256 * x4), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (16 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (64 + x5 + 256 * x4), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr0 + (32 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (128 + x5 + 256 * x4), xmask, eviction_policy=
'evict_last')
tmp11 = tl.load(in_ptr0 + (48 + x0 + 4 * x2 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp12 = tl.load(in_ptr1 + (192 + x5 + 256 * x4), xmask, eviction_policy
='evict_last')
tmp15 = tl.load(in_ptr2 + (x6 + 16 * x7), 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
tmp16 = tmp14 / tmp15
tmp17 = triton_helpers.maximum(tmp2, tmp5)
tmp18 = triton_helpers.maximum(tmp17, tmp9)
tmp19 = triton_helpers.maximum(tmp18, tmp13)
tl.store(out_ptr0 + (x5 + 128 * x7), tmp16, xmask)
tl.store(out_ptr1 + (x5 + 128 * x7), tmp19, 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, 4), (256, 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)
buf2 = empty_strided_cuda((4, 4, 8, 4, 4), (512, 128, 16, 4, 1),
torch.float32)
buf0 = reinterpret_tensor(buf2, (4, 4, 4, 4, 4), (512, 128, 16, 4,
1), 64)
buf1 = reinterpret_tensor(buf2, (4, 4, 4, 4, 4), (512, 128, 16, 4,
1), 0)
get_raw_stream(0)
triton_poi_fused_div_max_mul_sum_0[grid(1024)](arg0_1, arg1_1,
arg2_1, buf0, buf1, 1024, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
del arg2_1
return buf2,
class MeanMaxPoolingNew(nn.Module):
def __init__(self):
super(MeanMaxPoolingNew, self).__init__()
def forward(self, input_0, input_1, input_2):
arg1_1 = input_0
arg0_1 = input_1
arg2_1 = input_2
output = call([arg0_1, arg1_1, arg2_1])
return output[0]
|
jennybae1024/DFGN-pytorch
|
MeanMaxPooling
| false
| 15,681
|
[
"MIT"
] | 191
|
056d9317f772cd10bdd215bfafdbac5cbd330026
|
https://github.com/jennybae1024/DFGN-pytorch/tree/056d9317f772cd10bdd215bfafdbac5cbd330026
|
EmbeddingModel
|
import torch
class EmbeddingModel(torch.nn.Module):
@staticmethod
def forward(inputs):
return inputs.repeat(1, 10)
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_repeat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 160
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 40
x1 = xindex // 40
x2 = xindex
tmp0 = tl.load(in_ptr0 + (4 * x1 + x0 % 4), xmask)
tl.store(out_ptr0 + x2, tmp0, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 40), (40, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_repeat_0[grid(160)](arg0_1, buf0, 160, XBLOCK=256,
num_warps=4, num_stages=1)
del arg0_1
return buf0,
class EmbeddingModelNew(torch.nn.Module):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
jina-ai/finetuner
|
EmbeddingModel
| false
| 15,682
|
[
"Apache-2.0"
] | 270
|
6b8701c6ca372310364e6791c1c2761700dfc150
|
https://github.com/jina-ai/finetuner/tree/6b8701c6ca372310364e6791c1c2761700dfc150
|
MeanPooling
|
import torch
from torch import nn
class MeanPooling(nn.Module):
def __init__(self):
super(MeanPooling, self).__init__()
def forward(self, doc_state, entity_mapping, entity_lens):
entity_states = entity_mapping.unsqueeze(3) * doc_state.unsqueeze(1)
mean_pooled = torch.sum(entity_states, dim=2) / entity_lens.unsqueeze(2
)
return mean_pooled
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x4 = xindex // 16
x3 = xindex // 64
x5 = xindex % 16
x6 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + (x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr1 + (16 + x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr1 + (32 + x5 + 64 * x3), xmask, eviction_policy=
'evict_last')
tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x4), xmask, eviction_policy=
'evict_last')
tmp12 = tl.load(in_ptr1 + (48 + x5 + 64 * x3), 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 + x6, tmp14, xmask)
@triton.jit
def triton_poi_fused_div_mul_sum_1(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
x4 = xindex % 256
x0 = xindex % 16
x5 = xindex // 64
x6 = xindex
tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x0 + 16 * x5), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 / tmp1
tl.store(out_ptr0 + x6, tmp2, 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_mul_sum_0[grid(256)](arg0_1, arg1_1, buf0, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
torch.float32)
triton_poi_fused_div_mul_sum_1[grid(1024)](buf0, arg2_1, buf1, 1024,
XBLOCK=128, num_warps=4, num_stages=1)
del arg2_1
del buf0
return buf1,
class MeanPoolingNew(nn.Module):
def __init__(self):
super(MeanPoolingNew, self).__init__()
def forward(self, input_0, input_1, input_2):
arg0_1 = input_0
arg1_1 = input_1
arg2_1 = input_2
output = call([arg0_1, arg1_1, arg2_1])
return output[0]
|
jennybae1024/DFGN-pytorch
|
MeanPooling
| false
| 15,683
|
[
"MIT"
] | 191
|
056d9317f772cd10bdd215bfafdbac5cbd330026
|
https://github.com/jennybae1024/DFGN-pytorch/tree/056d9317f772cd10bdd215bfafdbac5cbd330026
|
DataProcessor
|
import torch
import torch.nn as nn
class DataProcessor(nn.Module):
def __init__(self):
super(DataProcessor, self).__init__()
self.pool = nn.AdaptiveAvgPool2d((7, 7))
def forward(self, x):
x = self.pool(x)
x = torch.squeeze(x)
x = x.permute(1, 2, 0)
return x.view(-1, x.size(-1))
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused__adaptive_avg_pool2d_0(in_ptr0, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 196
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 = 4 * x1 // 7
tmp1 = (10 + 4 * x1) // 7
tmp2 = tmp0 < tmp1
tmp3 = 4 * x0 // 7
tmp4 = (10 + 4 * x0) // 7
tmp5 = tmp3 < tmp4
tmp6 = tmp2 & tmp5
tmp7 = tl.load(in_ptr0 + (4 * (4 * x1 // 7) + 16 * x2 + 4 * x0 // 7),
tmp6 & xmask, eviction_policy='evict_last', other=0.0)
tmp8 = 1 + 4 * x0 // 7
tmp9 = tmp8 < tmp4
tmp10 = tmp2 & tmp9
tmp11 = tl.load(in_ptr0 + (1 + 4 * (4 * x1 // 7) + 16 * x2 + 4 * x0 //
7), tmp10 & xmask, eviction_policy='evict_last', other=0.0)
tmp12 = tmp11 + tmp7
tmp13 = 1 + 4 * x1 // 7
tmp14 = tmp13 < tmp1
tmp15 = tmp14 & tmp5
tmp16 = tl.load(in_ptr0 + (4 + 4 * (4 * x1 // 7) + 16 * x2 + 4 * x0 //
7), tmp15 & xmask, eviction_policy='evict_last', other=0.0)
tmp17 = tmp16 + tmp12
tmp18 = tmp14 & tmp9
tmp19 = tl.load(in_ptr0 + (5 + 4 * (4 * x1 // 7) + 16 * x2 + 4 * x0 //
7), tmp18 & xmask, eviction_policy='evict_last', other=0.0)
tmp20 = tmp19 + tmp17
tmp21 = 1.0
tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
tmp23 = tl.where(tmp6, tmp21, tmp22)
tmp24 = tl.where(tmp10, tmp21, tmp22)
tmp25 = tmp24 + tmp23
tmp26 = tl.where(tmp15, tmp21, tmp22)
tmp27 = tmp26 + tmp25
tmp28 = tl.where(tmp18, tmp21, tmp22)
tmp29 = tmp28 + tmp27
tmp30 = tmp20 / tmp29
tl.store(out_ptr0 + x4, tmp30, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 7, 7), (49, 7, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__adaptive_avg_pool2d_0[grid(196)](arg0_1, buf0,
196, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return reinterpret_tensor(buf0, (49, 4), (1, 49), 0),
class DataProcessorNew(nn.Module):
def __init__(self):
super(DataProcessorNew, self).__init__()
self.pool = nn.AdaptiveAvgPool2d((7, 7))
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
jianqingxie/RSTNet
|
DataProcessor
| false
| 15,684
|
[
"BSD-3-Clause"
] | 68
|
aaa7b5be08e5ec9e79e14ed3e6a04fc3d50483be
|
https://github.com/jianqingxie/RSTNet/tree/aaa7b5be08e5ec9e79e14ed3e6a04fc3d50483be
|
AddcmulTestModule
|
import torch
class AddcmulTestModule(torch.nn.Module):
def __init__(self, value):
super(AddcmulTestModule, self).__init__()
self.value = value
def forward(self, x, y, z):
return torch.addcmul(x, self.value, y, z)
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 [[], {'value': 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_addcmul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask)
tmp4 = tl.load(in_ptr2 + x0, xmask)
tmp2 = 4.0
tmp3 = tmp1 * tmp2
tmp5 = tmp3 * tmp4
tmp6 = tmp0 + tmp5
tl.store(out_ptr0 + x0, tmp6, xmask)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4, 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_addcmul_0[grid(256)](arg2_1, arg1_1, arg0_1, buf0,
256, XBLOCK=128, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
del arg2_1
return buf0,
class AddcmulTestModuleNew(torch.nn.Module):
def __init__(self, value):
super(AddcmulTestModuleNew, self).__init__()
self.value = value
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]
|
jinfagang/torch2trt_dynamic
|
AddcmulTestModule
| false
| 15,685
|
[
"MIT"
] | 155
|
fad7a7845f13cb59c05de25fcb83e7591acb492c
|
https://github.com/jinfagang/torch2trt_dynamic/tree/fad7a7845f13cb59c05de25fcb83e7591acb492c
|
HLoss
|
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.nn.functional as F
class HLoss(nn.Module):
def __init__(self):
super(HLoss, self).__init__()
def forward(self, x):
b = F.softmax(x, dim=1) * F.log_softmax(x, dim=1)
b = -1.0 * b.sum()
return b
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused__log_softmax__softmax_0(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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)
tl.store(out_ptr1 + x3, tmp8, xmask)
@triton.jit
def triton_per_fused__log_softmax__softmax_mul_sum_1(in_out_ptr0, in_ptr0,
in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r3 = rindex
r0 = rindex % 16
r2 = rindex // 64
tmp0 = tl.load(in_ptr0 + r3, None)
tmp1 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
)
tmp2 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp9 = tl.load(in_ptr1 + r3, None)
tmp10 = tl.load(in_ptr1 + (r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp12 = tl.load(in_ptr1 + (16 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp15 = tl.load(in_ptr1 + (32 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp18 = tl.load(in_ptr1 + (48 + r0 + 64 * r2), None, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tmp11 = tl_math.exp(tmp10)
tmp13 = tl_math.exp(tmp12)
tmp14 = tmp11 + tmp13
tmp16 = tl_math.exp(tmp15)
tmp17 = tmp14 + tmp16
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp17 + tmp19
tmp21 = tl_math.log(tmp20)
tmp22 = tmp9 - tmp21
tmp23 = tmp8 * tmp22
tmp24 = tl.broadcast_to(tmp23, [RBLOCK])
tmp26 = triton_helpers.promote_to_tensor(tl.sum(tmp24, 0))
tmp27 = -1.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, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__log_softmax__softmax_0[grid(256)](arg0_1, buf0,
buf1, 256, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
buf3 = empty_strided_cuda((), (), torch.float32)
buf4 = buf3
del buf3
triton_per_fused__log_softmax__softmax_mul_sum_1[grid(1)](buf4,
buf0, buf1, 1, 256, num_warps=2, num_stages=1)
del buf0
del buf1
return buf4,
class HLossNew(nn.Module):
def __init__(self):
super(HLossNew, self).__init__()
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
jfc43/robust-ood-detection
|
HLoss
| false
| 15,686
|
[
"Apache-2.0"
] | 55
|
fbeb63017f44b16b2911e61a1f7b7982a2621ee5
|
https://github.com/jfc43/robust-ood-detection/tree/fbeb63017f44b16b2911e61a1f7b7982a2621ee5
|
CoFusion
|
import torch
import torch.nn.functional as F
import torch.nn as nn
class CoFusion(nn.Module):
def __init__(self, in_ch, out_ch):
super(CoFusion, self).__init__()
self.conv1 = nn.Conv2d(in_ch, 64, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(64, out_ch, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.norm_layer1 = nn.GroupNorm(4, 64)
self.norm_layer2 = nn.GroupNorm(4, 64)
def forward(self, x):
attn = self.relu(self.norm_layer1(self.conv1(x)))
attn = self.relu(self.norm_layer2(self.conv2(attn)))
attn = F.softmax(self.conv3(attn), dim=1)
return (x * attn).sum(1).unsqueeze(1)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_ch': 4, 'out_ch': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_convolution_native_group_norm_0(in_out_ptr0, in_ptr0,
out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r5 = rindex
x4 = xindex
r3 = rindex // 16
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + (r5 + 256 * x4), None)
tmp1 = tl.load(in_ptr0 + (r3 + 16 * x0), None, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r5 + 256 * x4), tmp2, None)
tl.store(out_ptr2 + x4, tmp20, None)
tl.store(out_ptr0 + x4, tmp10, None)
tl.store(out_ptr1 + x4, tmp15, None)
@triton.jit
def triton_poi_fused_native_group_norm_relu_1(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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
x4 = xindex // 16
x1 = xindex // 16 % 64
tmp0 = tl.load(in_ptr0 + x3, None)
tmp1 = tl.load(in_ptr1 + x4 // 16, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x4 // 16, None, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr3 + x1, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr4 + x1, None, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = 256.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, None)
@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)
@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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_mul_sum_4(in_ptr0, in_ptr1, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
tmp2 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
tmp4 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
tmp6 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
tmp10 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
tmp14 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp1 / tmp7
tmp9 = tmp0 * tmp8
tmp11 = tmp2 / tmp7
tmp12 = tmp10 * tmp11
tmp13 = tmp9 + tmp12
tmp15 = tmp4 / tmp7
tmp16 = tmp14 * tmp15
tmp17 = tmp13 + tmp16
tmp19 = tmp6 / tmp7
tmp20 = tmp18 * tmp19
tmp21 = tmp17 + tmp20
tl.store(out_ptr0 + x2, tmp21, 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, (64, 4, 3, 3), (36, 9, 3, 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, (64,), (1,))
assert_size_stride(primals_5, (64,), (1,))
assert_size_stride(primals_6, (64, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_7, (64,), (1,))
assert_size_stride(primals_8, (64,), (1,))
assert_size_stride(primals_9, (64,), (1,))
assert_size_stride(primals_10, (4, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_11, (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, 64, 4, 4), (1024, 16, 4, 1))
buf1 = buf0
del buf0
buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf5 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
get_raw_stream(0)
triton_per_fused_convolution_native_group_norm_0[grid(16)](buf1,
primals_2, buf2, buf3, buf5, 16, 256, num_warps=2, num_stages=1)
del primals_2
buf6 = empty_strided_cuda((4, 64, 4, 4), (1024, 16, 4, 1), torch.
float32)
triton_poi_fused_native_group_norm_relu_1[grid(4096)](buf1, buf2,
buf3, primals_4, primals_5, buf6, 4096, XBLOCK=128, num_warps=4,
num_stages=1)
del primals_5
buf7 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf7, (4, 64, 4, 4), (1024, 16, 4, 1))
buf8 = buf7
del buf7
buf9 = buf3
del buf3
buf10 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf12 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
triton_per_fused_convolution_native_group_norm_0[grid(16)](buf8,
primals_7, buf9, buf10, buf12, 16, 256, num_warps=2, num_stages=1)
del primals_7
buf13 = empty_strided_cuda((4, 64, 4, 4), (1024, 16, 4, 1), torch.
float32)
triton_poi_fused_native_group_norm_relu_1[grid(4096)](buf8, buf9,
buf10, primals_8, primals_9, buf13, 4096, XBLOCK=128, num_warps
=4, num_stages=1)
del buf10
del primals_9
buf14 = extern_kernels.convolution(buf13, primals_10, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf14, (4, 4, 4, 4), (64, 16, 4, 1))
buf15 = buf14
del buf14
triton_poi_fused_convolution_2[grid(256)](buf15, primals_11, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_11
buf16 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_3[grid(256)](buf15, buf16, 256, XBLOCK=
256, num_warps=4, num_stages=1)
buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_mul_sum_4[grid(64)](primals_3, buf16,
buf17, 64, XBLOCK=64, num_warps=1, num_stages=1)
del buf16
return (reinterpret_tensor(buf17, (4, 1, 4, 4), (16, 16, 4, 1), 0),
primals_1, primals_3, primals_4, primals_6, primals_8, primals_10,
buf1, reinterpret_tensor(buf2, (4, 4), (4, 1), 0),
reinterpret_tensor(buf5, (4, 4), (4, 1), 0), buf6, buf8,
reinterpret_tensor(buf9, (4, 4), (4, 1), 0), reinterpret_tensor(
buf12, (4, 4), (4, 1), 0), buf13, buf15)
class CoFusionNew(nn.Module):
def __init__(self, in_ch, out_ch):
super(CoFusionNew, self).__init__()
self.conv1 = nn.Conv2d(in_ch, 64, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(64, out_ch, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.norm_layer1 = nn.GroupNorm(4, 64)
self.norm_layer2 = nn.GroupNorm(4, 64)
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_10 = self.conv3.weight
primals_11 = self.conv3.bias
primals_5 = self.norm_layer1.weight
primals_7 = self.norm_layer1.bias
primals_8 = self.norm_layer2.weight
primals_9 = self.norm_layer2.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]
|
jechague/DexiNed
|
CoFusion
| false
| 15,687
|
[
"MIT"
] | 471
|
370fe9031579b2d815ab706d7dc9daf23b969a87
|
https://github.com/jechague/DexiNed/tree/370fe9031579b2d815ab706d7dc9daf23b969a87
|
LBM
|
import torch
import torch.nn as nn
class LBM(nn.Module):
def __init__(self, l_dim, r_dim):
super(LBM, self).__init__()
self.W = nn.Bilinear(l_dim, r_dim, 1, bias=False)
def forward(self, e1, e2):
"""
e1: tensor of size (*, l_dim)
e2: tensor of size (*, r_dim)
return: tensor of size (*, 1)
"""
return torch.exp(self.W(e1, e2))
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'l_dim': 4, 'r_dim': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_exp_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_out_ptr0 + x0, xmask)
tmp1 = tl_math.exp(tmp0)
tl.store(in_out_ptr0 + x0, tmp1, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (1, 4, 4), (16, 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 = torch.ops.aten._trilinear.default(reinterpret_tensor(
primals_3, (64, 4), (4, 1), 0), primals_1, reinterpret_tensor(
primals_2, (64, 4), (4, 1), 0), [1, 3], [0], [1, 2], [2, 3])
del primals_1
buf1 = buf0
del buf0
buf2 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0)
del buf1
get_raw_stream(0)
triton_poi_fused_exp_0[grid(64)](buf2, 64, XBLOCK=64, num_warps=1,
num_stages=1)
return buf2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), buf2
class LBMNew(nn.Module):
def __init__(self, l_dim, r_dim):
super(LBMNew, self).__init__()
self.W = nn.Bilinear(l_dim, r_dim, 1, bias=False)
def forward(self, input_0, input_1):
primals_1 = self.W.weight
primals_2 = input_0
primals_3 = input_1
output = call([primals_1, primals_2, primals_3])
return output[0]
|
jinfenglin/TaxoExpan
|
LBM
| false
| 15,688
|
[
"Apache-2.0"
] | 55
|
86bd3f805508d03367539f2fdd43889fc0a4f6b2
|
https://github.com/jinfenglin/TaxoExpan/tree/86bd3f805508d03367539f2fdd43889fc0a4f6b2
|
ReCoNetMin
|
import torch
import numpy as np
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNetMin(SelectiveLoadModule):
def __init__(self):
super(ReCoNetMin, self).__init__()
self.style_conv1 = ConvInstRelu(3, 24, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(24, 48, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(48, 96, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(96, 96)
self.style_res2 = ResidualBlock(96, 96)
self.style_res3 = ResidualBlock(96, 96)
self.style_deconv1 = UpsampleConvInstRelu(96, 48, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(48, 24, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(24, 3, kernel_size=9, stride=1)
def forward(self, x):
return self.style_deconv3(self.style_deconv2(self.style_deconv1(
self.style_res3(self.style_res2(self.style_res1(self.
style_conv3(self.style_conv2(self.style_conv1(x)))))))))
def get_inputs():
return [torch.rand([4, 3, 64, 64])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 62208
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_red_fused__native_batch_norm_legit_convolution_1(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr,
RBLOCK: tl.constexpr):
xnumel = 96
rnumel = 4096
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rbase = tl.arange(0, RBLOCK)[None, :]
x3 = xindex
x0 = xindex % 24
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
for roffset in range(0, rnumel, RBLOCK):
rindex = roffset + rbase
rmask = rindex < rnumel
r2 = rindex
tmp0 = tl.load(in_out_ptr0 + (r2 + 4096 * x3), rmask & xmask,
eviction_policy='evict_first', other=0.0)
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers.
welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0)
)
tmp4_mean = tl.where(rmask & xmask, tmp4_mean_next, tmp4_mean)
tmp4_m2 = tl.where(rmask & xmask, tmp4_m2_next, tmp4_m2)
tmp4_weight = tl.where(rmask & xmask, tmp4_weight_next, tmp4_weight)
tl.store(in_out_ptr0 + (r2 + 4096 * x3), tmp2, rmask & xmask)
tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean,
tmp4_m2, tmp4_weight, 1)
tmp4 = tmp4_tmp[:, None]
tmp5 = tmp5_tmp[:, None]
tmp6_tmp[:, None]
tl.store(out_ptr0 + x3, tmp4, xmask)
tmp7 = 4096.0
tmp8 = tmp5 / tmp7
tmp9 = 1e-05
tmp10 = tmp8 + tmp9
tmp11 = libdevice.rsqrt(tmp10)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp11, xmask)
@triton.jit
def triton_poi_fused_repeat_2(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
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 24, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_3(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 418176
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 66
x1 = xindex // 66 % 66
x2 = xindex // 4356
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_4(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 48
tmp0 = tl.load(in_out_ptr0 + (r2 + 1024 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 1024, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 1024.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 1024 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 192
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 48, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 221952
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 34
x1 = xindex // 34 % 34
x2 = xindex // 1156
x3 = 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 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr2, out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 96
tmp0 = tl.load(in_ptr0 + x0 % 96, None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x0 % 96, None, eviction_policy='evict_last')
tmp2 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp3 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp5 = tl.broadcast_to(tmp4, [RBLOCK])
tmp7 = tl.broadcast_to(tmp5, [RBLOCK])
tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
tmp10 = tl.full([1], 256, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp5 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [RBLOCK])
tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
tmp18 = 256.0
tmp19 = tmp17 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp4 - tmp12
tmp24 = tmp23 * tmp22
tmp25 = tmp24 * tmp0
tmp26 = tmp25 + tmp1
tmp27 = tl.full([1], 0, tl.int32)
tmp28 = triton_helpers.maximum(tmp27, tmp26)
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(out_ptr1 + x0, tmp1, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp4, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x0, tmp22, None)
tl.store(out_ptr3 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr2 + x0, tmp12, None)
@triton.jit
def triton_poi_fused_reflection_pad2d_8(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 124416
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 18
x1 = xindex // 18 % 18
x2 = xindex // 324
x3 = xindex
tmp0 = tl.load(in_ptr0 + (255 + -1 * tl_math.abs(-15 + tl_math.abs(-1 +
x0)) + -16 * tl_math.abs(-15 + tl_math.abs(-1 + x1)) + 256 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_9(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 96
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_10(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 384
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 96, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_11(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 124416
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 18
x1 = xindex // 18 % 18
x2 = xindex // 324
x3 = xindex
tmp0 = tl.load(in_ptr0 + (255 + -1 * tl_math.abs(-15 + tl_math.abs(-1 +
x0)) + -16 * tl_math.abs(-15 + tl_math.abs(-1 + x1)) + 256 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 96
tmp0 = tl.load(in_ptr0 + x0 % 96, None, eviction_policy='evict_last')
tmp1 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp2 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last')
tmp25 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp27 = tl.load(in_out_ptr1 + (r3 + 256 * x0), None)
tmp3 = tmp1 + tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = tl.broadcast_to(tmp4, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = tl.full([1], 256, tl.int32)
tmp10 = tmp9.to(tl.float32)
tmp11 = tmp8 / tmp10
tmp12 = tmp4 - tmp11
tmp13 = tmp12 * tmp12
tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
tmp17 = tmp3 - tmp11
tmp18 = 256.0
tmp19 = tmp16 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp17 * tmp22
tmp24 = tmp23 * tmp0
tmp26 = tmp24 + tmp25
tmp28 = tmp26 + tmp27
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp3, None)
tl.store(in_out_ptr1 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr3 + x0, tmp22, None)
tl.store(out_ptr1 + x0, tmp11, None)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_13(in_out_ptr0,
in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 96
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.store(out_ptr2 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
tl.store(out_ptr1 + x3, tmp15, None)
@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_14(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_add_reflection_pad2d_15(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 443904
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 34 % 34
x0 = xindex % 34
x4 = xindex // 1156
x2 = xindex // 1156 % 96
x7 = xindex
tmp0 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 +
x1))), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 +
x0))), xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last')
tmp19 = tl.load(in_ptr4 + x4, xmask, eviction_policy='evict_last')
tmp21 = tl.load(in_ptr5 + x2, xmask, 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), xmask,
eviction_policy='evict_last')
tmp11 = tmp9 - tmp10
tmp13 = 256.0
tmp14 = tmp12 / tmp13
tmp15 = 1e-05
tmp16 = tmp14 + tmp15
tmp17 = libdevice.rsqrt(tmp16)
tmp18 = tmp11 * tmp17
tmp20 = tmp18 * tmp19
tmp22 = tmp20 + tmp21
tmp23 = tl.load(in_ptr6 + (tmp8 + 16 * tmp4 + 256 * x4), xmask,
eviction_policy='evict_last')
tmp24 = tmp22 + tmp23
tl.store(out_ptr0 + x7, tmp24, xmask)
@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_16(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_reflection_pad2d_relu_17(in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 836352
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 66 % 66
x0 = xindex % 66
x2 = xindex // 4356
x5 = 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')
tmp12 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last')
tmp16 = tl.load(in_ptr5 + 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 * x2), xmask,
eviction_policy='evict_last')
tmp11 = tmp9 - tmp10
tmp13 = tmp11 * tmp12
tmp15 = tmp13 * tmp14
tmp17 = tmp15 + tmp16
tmp18 = tl.full([1], 0, tl.int32)
tmp19 = triton_helpers.maximum(tmp18, tmp17)
tl.store(out_ptr0 + x5, tmp19, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_18(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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 % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, None, 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)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_add_convolution_div_mul_tanh_19(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x3 = xindex
x1 = xindex // 4096 % 3
tmp0 = tl.load(in_out_ptr0 + x3, None)
tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.00392156862745098
tmp4 = tmp2 * tmp3
tmp5 = libdevice.tanh(tmp4)
tmp6 = 150.0
tmp7 = tmp5 * tmp6
tmp8 = 127.5
tmp9 = tmp7 + tmp8
tl.store(in_out_ptr0 + x3, tmp2, None)
tl.store(out_ptr0 + x3, tmp9, None)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17,
primals_18, primals_19, primals_20, primals_21, primals_22,
primals_23, primals_24, primals_25, primals_26, primals_27,
primals_28, primals_29, primals_30, primals_31, primals_32,
primals_33, primals_34, primals_35, primals_36, primals_37,
primals_38, primals_39, primals_40, primals_41, primals_42,
primals_43, primals_44, primals_45, primals_46, primals_47) = args
args.clear()
assert_size_stride(primals_1, (4, 3, 64, 64), (12288, 4096, 64, 1))
assert_size_stride(primals_2, (24, 3, 9, 9), (243, 81, 9, 1))
assert_size_stride(primals_3, (24,), (1,))
assert_size_stride(primals_4, (24,), (1,))
assert_size_stride(primals_5, (24,), (1,))
assert_size_stride(primals_6, (48, 24, 3, 3), (216, 9, 3, 1))
assert_size_stride(primals_7, (48,), (1,))
assert_size_stride(primals_8, (48,), (1,))
assert_size_stride(primals_9, (48,), (1,))
assert_size_stride(primals_10, (96, 48, 3, 3), (432, 9, 3, 1))
assert_size_stride(primals_11, (96,), (1,))
assert_size_stride(primals_12, (96,), (1,))
assert_size_stride(primals_13, (96,), (1,))
assert_size_stride(primals_14, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_15, (96,), (1,))
assert_size_stride(primals_16, (96,), (1,))
assert_size_stride(primals_17, (96,), (1,))
assert_size_stride(primals_18, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_19, (96,), (1,))
assert_size_stride(primals_20, (96,), (1,))
assert_size_stride(primals_21, (96,), (1,))
assert_size_stride(primals_22, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_23, (96,), (1,))
assert_size_stride(primals_24, (96,), (1,))
assert_size_stride(primals_25, (96,), (1,))
assert_size_stride(primals_26, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_27, (96,), (1,))
assert_size_stride(primals_28, (96,), (1,))
assert_size_stride(primals_29, (96,), (1,))
assert_size_stride(primals_30, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_31, (96,), (1,))
assert_size_stride(primals_32, (96,), (1,))
assert_size_stride(primals_33, (96,), (1,))
assert_size_stride(primals_34, (96, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_35, (96,), (1,))
assert_size_stride(primals_36, (96,), (1,))
assert_size_stride(primals_37, (96,), (1,))
assert_size_stride(primals_38, (48, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_39, (48,), (1,))
assert_size_stride(primals_40, (48,), (1,))
assert_size_stride(primals_41, (48,), (1,))
assert_size_stride(primals_42, (24, 48, 3, 3), (432, 9, 3, 1))
assert_size_stride(primals_43, (24,), (1,))
assert_size_stride(primals_44, (24,), (1,))
assert_size_stride(primals_45, (24,), (1,))
assert_size_stride(primals_46, (3, 24, 9, 9), (1944, 81, 9, 1))
assert_size_stride(primals_47, (3,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 3, 72, 72), (15552, 5184, 72, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_reflection_pad2d_0[grid(62208)](primals_1, buf0,
62208, XBLOCK=256, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 24, 64, 64), (98304, 4096, 64, 1))
buf2 = buf1
del buf1
buf5 = empty_strided_cuda((1, 96, 1, 1), (96, 1, 1, 1), torch.float32)
buf6 = empty_strided_cuda((1, 96, 1, 1), (96, 1, 96, 96), torch.float32
)
buf8 = reinterpret_tensor(buf6, (1, 96, 1, 1), (96, 1, 1, 1), 0)
del buf6
triton_red_fused__native_batch_norm_legit_convolution_1[grid(96)](buf2,
buf8, primals_3, buf5, 96, 4096, XBLOCK=1, RBLOCK=2048,
num_warps=16, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((96,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(96)](primals_4, buf3, 96, XBLOCK=128,
num_warps=4, num_stages=1)
del primals_4
buf4 = empty_strided_cuda((96,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(96)](primals_5, buf4, 96, XBLOCK=128,
num_warps=4, num_stages=1)
del primals_5
buf9 = empty_strided_cuda((4, 24, 66, 66), (104544, 4356, 66, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_3[grid(418176)](buf2, buf5,
buf8, buf3, buf4, buf9, 418176, XBLOCK=512, num_warps=8,
num_stages=1)
buf10 = extern_kernels.convolution(buf9, primals_6, stride=(2, 2),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf10, (4, 48, 32, 32), (49152, 1024, 32, 1))
buf11 = buf10
del buf10
buf14 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 1, 1), torch.
float32)
buf15 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 192, 192),
torch.float32)
buf17 = reinterpret_tensor(buf15, (1, 192, 1, 1), (192, 1, 1, 1), 0)
del buf15
triton_per_fused__native_batch_norm_legit_convolution_4[grid(192)](
buf11, buf17, primals_7, buf14, 192, 1024, num_warps=8,
num_stages=1)
del primals_7
buf12 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(192)](primals_8, buf12, 192, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_8
buf13 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(192)](primals_9, buf13, 192, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_9
buf18 = empty_strided_cuda((4, 48, 34, 34), (55488, 1156, 34, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_6[grid(221952)](buf11, buf14,
buf17, buf12, buf13, buf18, 221952, XBLOCK=1024, num_warps=4,
num_stages=1)
buf19 = extern_kernels.convolution(buf18, primals_10, stride=(2, 2),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf19, (4, 96, 16, 16), (24576, 256, 16, 1))
buf21 = empty_strided_cuda((384,), (1,), torch.float32)
buf22 = empty_strided_cuda((384,), (1,), torch.float32)
buf20 = buf19
del buf19
buf23 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf24 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf26 = reinterpret_tensor(buf24, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf24
buf27 = empty_strided_cuda((4, 96, 16, 16), (24576, 256, 16, 1),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7[
grid(384)](buf20, buf26, primals_12, primals_13, primals_11,
buf21, buf22, buf23, buf27, 384, 256, num_warps=2, num_stages=1)
del primals_11
del primals_12
del primals_13
buf28 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(124416)](buf27, buf28,
124416, XBLOCK=1024, num_warps=4, num_stages=1)
buf29 = extern_kernels.convolution(buf28, primals_14, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf29, (4, 96, 16, 16), (24576, 256, 16, 1))
buf30 = buf29
del buf29
buf33 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf34 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf36 = reinterpret_tensor(buf34, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf34
triton_per_fused__native_batch_norm_legit_convolution_9[grid(384)](
buf30, buf36, primals_15, buf33, 384, 256, num_warps=2,
num_stages=1)
del primals_15
buf31 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_16, buf31, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_16
buf32 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_17, buf32, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_17
buf37 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(124416)](buf30,
buf33, buf36, buf31, buf32, buf37, 124416, XBLOCK=512,
num_warps=8, num_stages=1)
buf38 = extern_kernels.convolution(buf37, primals_18, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf38, (4, 96, 16, 16), (24576, 256, 16, 1))
buf40 = empty_strided_cuda((384,), (1,), torch.float32)
buf39 = buf38
del buf38
buf41 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf45 = buf27
del buf27
buf44 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(384)](buf39, buf45, primals_20, primals_19, primals_21,
buf40, buf41, buf44, 384, 256, num_warps=2, num_stages=1)
del primals_19
del primals_20
del primals_21
buf46 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(124416)](buf45, buf46,
124416, XBLOCK=1024, num_warps=4, num_stages=1)
buf47 = extern_kernels.convolution(buf46, primals_22, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf47, (4, 96, 16, 16), (24576, 256, 16, 1))
buf48 = buf47
del buf47
buf51 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf52 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf54 = reinterpret_tensor(buf52, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf52
triton_per_fused__native_batch_norm_legit_convolution_9[grid(384)](
buf48, buf54, primals_23, buf51, 384, 256, num_warps=2,
num_stages=1)
del primals_23
buf49 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_24, buf49, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_24
buf50 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_25, buf50, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_25
buf55 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(124416)](buf48,
buf51, buf54, buf49, buf50, buf55, 124416, XBLOCK=512,
num_warps=8, num_stages=1)
buf56 = extern_kernels.convolution(buf55, primals_26, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf56, (4, 96, 16, 16), (24576, 256, 16, 1))
buf58 = empty_strided_cuda((384,), (1,), torch.float32)
buf57 = buf56
del buf56
buf59 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf63 = buf45
del buf45
buf62 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(384)](buf57, buf63, primals_28, primals_27, primals_29,
buf58, buf59, buf62, 384, 256, num_warps=2, num_stages=1)
del primals_27
del primals_28
del primals_29
buf64 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(124416)](buf63, buf64,
124416, XBLOCK=1024, num_warps=4, num_stages=1)
buf65 = extern_kernels.convolution(buf64, primals_30, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf65, (4, 96, 16, 16), (24576, 256, 16, 1))
buf66 = buf65
del buf65
buf69 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf70 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf72 = reinterpret_tensor(buf70, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf70
triton_per_fused__native_batch_norm_legit_convolution_9[grid(384)](
buf66, buf72, primals_31, buf69, 384, 256, num_warps=2,
num_stages=1)
del primals_31
buf67 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_32, buf67, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_32
buf68 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_33, buf68, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_33
buf73 = empty_strided_cuda((4, 96, 18, 18), (31104, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(124416)](buf66,
buf69, buf72, buf67, buf68, buf73, 124416, XBLOCK=512,
num_warps=8, num_stages=1)
buf74 = extern_kernels.convolution(buf73, primals_34, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf74, (4, 96, 16, 16), (24576, 256, 16, 1))
buf75 = buf74
del buf74
buf77 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf78 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf80 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_13[grid(384)](
buf75, primals_35, buf77, buf78, buf80, 384, 256, num_warps=2,
num_stages=1)
del primals_35
buf76 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(384)](primals_36, buf76, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_36
buf81 = empty_strided_cuda((32,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_14[grid(32)](buf81, 32,
XBLOCK=32, num_warps=1, num_stages=1)
buf82 = empty_strided_cuda((4, 96, 34, 34), (110976, 1156, 34, 1),
torch.float32)
triton_poi_fused__unsafe_index_add_reflection_pad2d_15[grid(443904)](
buf81, buf75, buf77, buf78, buf76, primals_37, buf63, buf82,
443904, XBLOCK=1024, num_warps=4, num_stages=1)
del buf63
del buf78
del primals_37
buf83 = extern_kernels.convolution(buf82, primals_38, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf83, (4, 48, 32, 32), (49152, 1024, 32, 1))
buf84 = buf83
del buf83
buf87 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 1, 1), torch.
float32)
buf88 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 192, 192),
torch.float32)
buf90 = reinterpret_tensor(buf88, (1, 192, 1, 1), (192, 1, 1, 1), 0)
del buf88
triton_per_fused__native_batch_norm_legit_convolution_4[grid(192)](
buf84, buf90, primals_39, buf87, 192, 1024, num_warps=8,
num_stages=1)
del primals_39
buf85 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(192)](primals_40, buf85, 192, XBLOCK
=256, num_warps=4, num_stages=1)
del primals_40
buf86 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(192)](primals_41, buf86, 192, XBLOCK
=256, num_warps=4, num_stages=1)
del primals_41
buf91 = empty_strided_cuda((64,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_16[grid(64)](buf91, 64,
XBLOCK=64, num_warps=1, num_stages=1)
buf92 = empty_strided_cuda((4, 48, 66, 66), (209088, 4356, 66, 1),
torch.float32)
triton_poi_fused__unsafe_index_reflection_pad2d_relu_17[grid(836352)](
buf91, buf84, buf87, buf90, buf85, buf86, buf92, 836352, XBLOCK
=512, num_warps=8, num_stages=1)
buf93 = extern_kernels.convolution(buf92, primals_42, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf93, (4, 24, 64, 64), (98304, 4096, 64, 1))
buf94 = buf93
del buf93
buf97 = empty_strided_cuda((1, 96, 1, 1), (96, 1, 1, 1), torch.float32)
buf98 = empty_strided_cuda((1, 96, 1, 1), (96, 1, 96, 96), torch.
float32)
buf100 = reinterpret_tensor(buf98, (1, 96, 1, 1), (96, 1, 1, 1), 0)
del buf98
triton_red_fused__native_batch_norm_legit_convolution_1[grid(96)](buf94
, buf100, primals_43, buf97, 96, 4096, XBLOCK=1, RBLOCK=2048,
num_warps=16, num_stages=1)
del primals_43
buf95 = empty_strided_cuda((96,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(96)](primals_44, buf95, 96, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_44
buf96 = empty_strided_cuda((96,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(96)](primals_45, buf96, 96, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_45
buf101 = empty_strided_cuda((4, 24, 72, 72), (124416, 5184, 72, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_18[grid(497664)](buf94,
buf97, buf100, buf95, buf96, buf101, 497664, XBLOCK=1024,
num_warps=4, num_stages=1)
buf102 = extern_kernels.convolution(buf101, primals_46, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf102, (4, 3, 64, 64), (12288, 4096, 64, 1))
buf103 = buf102
del buf102
buf104 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1),
torch.float32)
triton_poi_fused_add_convolution_div_mul_tanh_19[grid(49152)](buf103,
primals_47, buf104, 49152, XBLOCK=512, num_warps=4, num_stages=1)
del primals_47
return (buf104, primals_2, primals_6, primals_10, primals_14,
primals_18, primals_22, primals_26, primals_30, primals_34,
primals_38, primals_42, primals_46, buf0, buf2, buf3, buf4, buf5,
buf8, buf9, buf11, buf12, buf13, buf14, buf17, buf18, buf20, buf21,
buf22, buf23, buf26, buf28, buf30, buf31, buf32, buf33, buf36,
buf37, buf39, buf40, reinterpret_tensor(buf44, (384,), (1,), 0),
buf46, buf48, buf49, buf50, buf51, buf54, buf55, buf57, buf58,
reinterpret_tensor(buf62, (384,), (1,), 0), buf64, buf66, buf67,
buf68, buf69, buf72, buf73, buf75, buf76, reinterpret_tensor(buf80,
(384,), (1,), 0), buf81, buf82, buf84, buf85, buf86, buf87, buf90,
buf91, buf92, buf94, buf95, buf96, buf97, buf100, buf101, buf103,
reinterpret_tensor(buf77, (1, 384, 1, 1), (384, 1, 1, 1), 0),
reinterpret_tensor(buf59, (1, 384, 1, 1), (384, 1, 1, 1), 0),
reinterpret_tensor(buf41, (1, 384, 1, 1), (384, 1, 1, 1), 0))
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNetMinNew(SelectiveLoadModule):
def __init__(self):
super(ReCoNetMinNew, self).__init__()
self.style_conv1 = ConvInstRelu(3, 24, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(24, 48, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(48, 96, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(96, 96)
self.style_res2 = ResidualBlock(96, 96)
self.style_res3 = ResidualBlock(96, 96)
self.style_deconv1 = UpsampleConvInstRelu(96, 48, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(48, 24, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(24, 3, kernel_size=9, stride=1)
def forward(self, input_0):
primals_2 = self.style_conv1.conv2d.weight
primals_3 = self.style_conv1.conv2d.bias
primals_4 = self.style_conv1.instance.weight
primals_5 = self.style_conv1.instance.bias
primals_6 = self.style_conv2.conv2d.weight
primals_7 = self.style_conv2.conv2d.bias
primals_8 = self.style_conv2.instance.weight
primals_9 = self.style_conv2.instance.bias
primals_10 = self.style_conv3.conv2d.weight
primals_11 = self.style_conv3.conv2d.bias
primals_12 = self.style_conv3.instance.weight
primals_13 = self.style_conv3.instance.bias
primals_14 = self.style_res1.conv1.conv2d.weight
primals_15 = self.style_res1.conv1.conv2d.bias
primals_16 = self.style_res1.in1.weight
primals_17 = self.style_res1.in1.bias
primals_18 = self.style_res1.conv2.conv2d.weight
primals_19 = self.style_res1.conv2.conv2d.bias
primals_20 = self.style_res1.in2.weight
primals_21 = self.style_res1.in2.bias
primals_22 = self.style_res2.conv1.conv2d.weight
primals_23 = self.style_res2.conv1.conv2d.bias
primals_24 = self.style_res2.in1.weight
primals_25 = self.style_res2.in1.bias
primals_26 = self.style_res2.conv2.conv2d.weight
primals_27 = self.style_res2.conv2.conv2d.bias
primals_28 = self.style_res2.in2.weight
primals_29 = self.style_res2.in2.bias
primals_30 = self.style_res3.conv1.conv2d.weight
primals_31 = self.style_res3.conv1.conv2d.bias
primals_32 = self.style_res3.in1.weight
primals_33 = self.style_res3.in1.bias
primals_34 = self.style_res3.conv2.conv2d.weight
primals_35 = self.style_res3.conv2.conv2d.bias
primals_36 = self.style_res3.in2.weight
primals_37 = self.style_res3.in2.bias
primals_38 = self.style_deconv1.conv2d.weight
primals_39 = self.style_deconv1.conv2d.bias
primals_40 = self.style_deconv1.instance.weight
primals_41 = self.style_deconv1.instance.bias
primals_42 = self.style_deconv2.conv2d.weight
primals_43 = self.style_deconv2.conv2d.bias
primals_44 = self.style_deconv2.instance.weight
primals_45 = self.style_deconv2.instance.bias
primals_46 = self.style_deconv3.conv2d.weight
primals_47 = self.style_deconv3.conv2d.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20, primals_21, primals_22, primals_23, primals_24,
primals_25, primals_26, primals_27, primals_28, primals_29,
primals_30, primals_31, primals_32, primals_33, primals_34,
primals_35, primals_36, primals_37, primals_38, primals_39,
primals_40, primals_41, primals_42, primals_43, primals_44,
primals_45, primals_46, primals_47])
return output[0]
|
irsisyphus/reconet
|
ReCoNetMin
| false
| 15,689
|
[
"MIT"
] | 56
|
863acf8dde4d45c8521634af27878fe04f3b2e56
|
https://github.com/irsisyphus/reconet/tree/863acf8dde4d45c8521634af27878fe04f3b2e56
|
ReCoNet2
|
import torch
import numpy as np
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNet2(SelectiveLoadModule):
def __init__(self):
super(ReCoNet2, self).__init__()
self.style_conv1 = ConvInstRelu(3, 48, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(48, 96, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(96, 192, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(192, 192)
self.style_res2 = ResidualBlock(192, 192)
self.style_res3 = ResidualBlock(192, 192)
self.style_res4 = ResidualBlock(192, 192)
self.style_deconv1 = UpsampleConvInstRelu(192, 96, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(96, 48, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(48, 3, kernel_size=9, stride=1)
def forward(self, x):
return self.style_deconv3(self.style_deconv2(self.style_deconv1(
self.style_res4(self.style_res3(self.style_res2(self.style_res1
(self.style_conv3(self.style_conv2(self.style_conv1(x))))))))))
def get_inputs():
return [torch.rand([4, 3, 64, 64])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 62208
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_red_fused__native_batch_norm_legit_convolution_1(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr,
RBLOCK: tl.constexpr):
xnumel = 192
rnumel = 4096
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rbase = tl.arange(0, RBLOCK)[None, :]
x3 = xindex
x0 = xindex % 48
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
for roffset in range(0, rnumel, RBLOCK):
rindex = roffset + rbase
rmask = rindex < rnumel
r2 = rindex
tmp0 = tl.load(in_out_ptr0 + (r2 + 4096 * x3), rmask & xmask,
eviction_policy='evict_first', other=0.0)
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers.
welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0)
)
tmp4_mean = tl.where(rmask & xmask, tmp4_mean_next, tmp4_mean)
tmp4_m2 = tl.where(rmask & xmask, tmp4_m2_next, tmp4_m2)
tmp4_weight = tl.where(rmask & xmask, tmp4_weight_next, tmp4_weight)
tl.store(in_out_ptr0 + (r2 + 4096 * x3), tmp2, rmask & xmask)
tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean,
tmp4_m2, tmp4_weight, 1)
tmp4 = tmp4_tmp[:, None]
tmp5 = tmp5_tmp[:, None]
tmp6_tmp[:, None]
tl.store(out_ptr0 + x3, tmp4, xmask)
tmp7 = 4096.0
tmp8 = tmp5 / tmp7
tmp9 = 1e-05
tmp10 = tmp8 + tmp9
tmp11 = libdevice.rsqrt(tmp10)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp11, xmask)
@triton.jit
def triton_poi_fused_repeat_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 192
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 48, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_3(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 836352
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 66
x1 = xindex // 66 % 66
x2 = xindex // 4356
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_4(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 96
tmp0 = tl.load(in_out_ptr0 + (r2 + 1024 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 1024, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 1024.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 1024 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 384
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 96, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 443904
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 34
x1 = xindex // 34 % 34
x2 = xindex // 1156
x3 = 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 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr2, out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 192
tmp0 = tl.load(in_ptr0 + x0 % 192, None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x0 % 192, None, eviction_policy='evict_last')
tmp2 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp3 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp5 = tl.broadcast_to(tmp4, [RBLOCK])
tmp7 = tl.broadcast_to(tmp5, [RBLOCK])
tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
tmp10 = tl.full([1], 256, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp5 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [RBLOCK])
tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
tmp18 = 256.0
tmp19 = tmp17 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp4 - tmp12
tmp24 = tmp23 * tmp22
tmp25 = tmp24 * tmp0
tmp26 = tmp25 + tmp1
tmp27 = tl.full([1], 0, tl.int32)
tmp28 = triton_helpers.maximum(tmp27, tmp26)
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(out_ptr1 + x0, tmp1, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp4, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x0, tmp22, None)
tl.store(out_ptr3 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr2 + x0, tmp12, None)
@triton.jit
def triton_poi_fused_reflection_pad2d_8(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 248832
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 18
x1 = xindex // 18 % 18
x2 = xindex // 324
x3 = xindex
tmp0 = tl.load(in_ptr0 + (255 + -1 * tl_math.abs(-15 + tl_math.abs(-1 +
x0)) + -16 * tl_math.abs(-15 + tl_math.abs(-1 + x1)) + 256 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_9(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 192
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_repeat_10(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 768
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 192, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_11(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 248832
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 18
x1 = xindex // 18 % 18
x2 = xindex // 324
x3 = xindex
tmp0 = tl.load(in_ptr0 + (255 + -1 * tl_math.abs(-15 + tl_math.abs(-1 +
x0)) + -16 * tl_math.abs(-15 + tl_math.abs(-1 + x1)) + 256 * x2),
xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, 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)
tl.store(out_ptr0 + x3, tmp10, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12(
in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
out_ptr3, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 192
tmp0 = tl.load(in_ptr0 + x0 % 192, None, eviction_policy='evict_last')
tmp1 = tl.load(in_out_ptr0 + (r3 + 256 * x0), None)
tmp2 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last')
tmp25 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
tmp27 = tl.load(in_out_ptr1 + (r3 + 256 * x0), None)
tmp3 = tmp1 + tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = tl.broadcast_to(tmp4, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = tl.full([1], 256, tl.int32)
tmp10 = tmp9.to(tl.float32)
tmp11 = tmp8 / tmp10
tmp12 = tmp4 - tmp11
tmp13 = tmp12 * tmp12
tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
tmp17 = tmp3 - tmp11
tmp18 = 256.0
tmp19 = tmp16 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp17 * tmp22
tmp24 = tmp23 * tmp0
tmp26 = tmp24 + tmp25
tmp28 = tmp26 + tmp27
tl.store(out_ptr0 + x0, tmp0, None)
tl.store(in_out_ptr0 + (r3 + 256 * x0), tmp3, None)
tl.store(in_out_ptr1 + (r3 + 256 * x0), tmp28, None)
tl.store(out_ptr3 + x0, tmp22, None)
tl.store(out_ptr1 + x0, tmp11, None)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_13(in_out_ptr0,
in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 192
tmp0 = tl.load(in_out_ptr0 + (r2 + 256 * x3), None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
tmp8 = tl.full([1], 256, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp3 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tmp18 = 1e-05
tmp19 = tmp17 + tmp18
tmp20 = libdevice.rsqrt(tmp19)
tl.store(in_out_ptr0 + (r2 + 256 * x3), tmp2, None)
tl.store(out_ptr2 + x3, tmp20, None)
tl.store(out_ptr0 + x3, tmp10, None)
tl.store(out_ptr1 + x3, tmp15, None)
@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_14(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_add_reflection_pad2d_15(in_ptr0, in_ptr1,
in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 887808
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 34 % 34
x0 = xindex % 34
x4 = xindex // 1156
x2 = xindex // 1156 % 192
x7 = xindex
tmp0 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 +
x1))), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 +
x0))), xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last')
tmp19 = tl.load(in_ptr4 + x4, xmask, eviction_policy='evict_last')
tmp21 = tl.load(in_ptr5 + x2, xmask, 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), xmask,
eviction_policy='evict_last')
tmp11 = tmp9 - tmp10
tmp13 = 256.0
tmp14 = tmp12 / tmp13
tmp15 = 1e-05
tmp16 = tmp14 + tmp15
tmp17 = libdevice.rsqrt(tmp16)
tmp18 = tmp11 * tmp17
tmp20 = tmp18 * tmp19
tmp22 = tmp20 + tmp21
tmp23 = tl.load(in_ptr6 + (tmp8 + 16 * tmp4 + 256 * x4), xmask,
eviction_policy='evict_last')
tmp24 = tmp22 + tmp23
tl.store(out_ptr0 + x7, tmp24, xmask)
@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_16(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_reflection_pad2d_relu_17(in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 1672704
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 66 % 66
x0 = xindex % 66
x2 = xindex // 4356
x5 = 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')
tmp12 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last')
tmp16 = tl.load(in_ptr5 + 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 * x2), xmask,
eviction_policy='evict_last')
tmp11 = tmp9 - tmp10
tmp13 = tmp11 * tmp12
tmp15 = tmp13 * tmp14
tmp17 = tmp15 + tmp16
tmp18 = tl.full([1], 0, tl.int32)
tmp19 = triton_helpers.maximum(tmp18, tmp17)
tl.store(out_ptr0 + x5, tmp19, xmask)
@triton.jit
def triton_poi_fused_reflection_pad2d_relu_18(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, 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 % 72
x1 = xindex // 72 % 72
x2 = xindex // 5184
x3 = xindex
tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 +
x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2),
None, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x2, None, 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)
tl.store(out_ptr0 + x3, tmp10, None)
@triton.jit
def triton_poi_fused_add_convolution_div_mul_tanh_19(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x3 = xindex
x1 = xindex // 4096 % 3
tmp0 = tl.load(in_out_ptr0 + x3, None)
tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.00392156862745098
tmp4 = tmp2 * tmp3
tmp5 = libdevice.tanh(tmp4)
tmp6 = 150.0
tmp7 = tmp5 * tmp6
tmp8 = 127.5
tmp9 = tmp7 + tmp8
tl.store(in_out_ptr0 + x3, tmp2, None)
tl.store(out_ptr0 + x3, tmp9, None)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17,
primals_18, primals_19, primals_20, primals_21, primals_22,
primals_23, primals_24, primals_25, primals_26, primals_27,
primals_28, primals_29, primals_30, primals_31, primals_32,
primals_33, primals_34, primals_35, primals_36, primals_37,
primals_38, primals_39, primals_40, primals_41, primals_42,
primals_43, primals_44, primals_45, primals_46, primals_47,
primals_48, primals_49, primals_50, primals_51, primals_52,
primals_53, primals_54, primals_55) = args
args.clear()
assert_size_stride(primals_1, (4, 3, 64, 64), (12288, 4096, 64, 1))
assert_size_stride(primals_2, (48, 3, 9, 9), (243, 81, 9, 1))
assert_size_stride(primals_3, (48,), (1,))
assert_size_stride(primals_4, (48,), (1,))
assert_size_stride(primals_5, (48,), (1,))
assert_size_stride(primals_6, (96, 48, 3, 3), (432, 9, 3, 1))
assert_size_stride(primals_7, (96,), (1,))
assert_size_stride(primals_8, (96,), (1,))
assert_size_stride(primals_9, (96,), (1,))
assert_size_stride(primals_10, (192, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_11, (192,), (1,))
assert_size_stride(primals_12, (192,), (1,))
assert_size_stride(primals_13, (192,), (1,))
assert_size_stride(primals_14, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_15, (192,), (1,))
assert_size_stride(primals_16, (192,), (1,))
assert_size_stride(primals_17, (192,), (1,))
assert_size_stride(primals_18, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_19, (192,), (1,))
assert_size_stride(primals_20, (192,), (1,))
assert_size_stride(primals_21, (192,), (1,))
assert_size_stride(primals_22, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_23, (192,), (1,))
assert_size_stride(primals_24, (192,), (1,))
assert_size_stride(primals_25, (192,), (1,))
assert_size_stride(primals_26, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_27, (192,), (1,))
assert_size_stride(primals_28, (192,), (1,))
assert_size_stride(primals_29, (192,), (1,))
assert_size_stride(primals_30, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_31, (192,), (1,))
assert_size_stride(primals_32, (192,), (1,))
assert_size_stride(primals_33, (192,), (1,))
assert_size_stride(primals_34, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_35, (192,), (1,))
assert_size_stride(primals_36, (192,), (1,))
assert_size_stride(primals_37, (192,), (1,))
assert_size_stride(primals_38, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_39, (192,), (1,))
assert_size_stride(primals_40, (192,), (1,))
assert_size_stride(primals_41, (192,), (1,))
assert_size_stride(primals_42, (192, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_43, (192,), (1,))
assert_size_stride(primals_44, (192,), (1,))
assert_size_stride(primals_45, (192,), (1,))
assert_size_stride(primals_46, (96, 192, 3, 3), (1728, 9, 3, 1))
assert_size_stride(primals_47, (96,), (1,))
assert_size_stride(primals_48, (96,), (1,))
assert_size_stride(primals_49, (96,), (1,))
assert_size_stride(primals_50, (48, 96, 3, 3), (864, 9, 3, 1))
assert_size_stride(primals_51, (48,), (1,))
assert_size_stride(primals_52, (48,), (1,))
assert_size_stride(primals_53, (48,), (1,))
assert_size_stride(primals_54, (3, 48, 9, 9), (3888, 81, 9, 1))
assert_size_stride(primals_55, (3,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 3, 72, 72), (15552, 5184, 72, 1),
torch.float32)
get_raw_stream(0)
triton_poi_fused_reflection_pad2d_0[grid(62208)](primals_1, buf0,
62208, XBLOCK=256, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 48, 64, 64), (196608, 4096, 64, 1))
buf2 = buf1
del buf1
buf5 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 1, 1), torch.float32
)
buf6 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 192, 192), torch
.float32)
buf8 = reinterpret_tensor(buf6, (1, 192, 1, 1), (192, 1, 1, 1), 0)
del buf6
triton_red_fused__native_batch_norm_legit_convolution_1[grid(192)](buf2
, buf8, primals_3, buf5, 192, 4096, XBLOCK=1, RBLOCK=2048,
num_warps=16, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(192)](primals_4, buf3, 192, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_4
buf4 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(192)](primals_5, buf4, 192, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_5
buf9 = empty_strided_cuda((4, 48, 66, 66), (209088, 4356, 66, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_3[grid(836352)](buf2, buf5,
buf8, buf3, buf4, buf9, 836352, XBLOCK=512, num_warps=8,
num_stages=1)
buf10 = extern_kernels.convolution(buf9, primals_6, stride=(2, 2),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf10, (4, 96, 32, 32), (98304, 1024, 32, 1))
buf11 = buf10
del buf10
buf14 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf15 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf17 = reinterpret_tensor(buf15, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf15
triton_per_fused__native_batch_norm_legit_convolution_4[grid(384)](
buf11, buf17, primals_7, buf14, 384, 1024, num_warps=8,
num_stages=1)
del primals_7
buf12 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(384)](primals_8, buf12, 384, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_8
buf13 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(384)](primals_9, buf13, 384, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_9
buf18 = empty_strided_cuda((4, 96, 34, 34), (110976, 1156, 34, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_6[grid(443904)](buf11, buf14,
buf17, buf12, buf13, buf18, 443904, XBLOCK=1024, num_warps=4,
num_stages=1)
buf19 = extern_kernels.convolution(buf18, primals_10, stride=(2, 2),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf19, (4, 192, 16, 16), (49152, 256, 16, 1))
buf21 = empty_strided_cuda((768,), (1,), torch.float32)
buf22 = empty_strided_cuda((768,), (1,), torch.float32)
buf20 = buf19
del buf19
buf23 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 1, 1), torch.
float32)
buf24 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf26 = reinterpret_tensor(buf24, (1, 768, 1, 1), (768, 1, 1, 1), 0)
del buf24
buf27 = empty_strided_cuda((4, 192, 16, 16), (49152, 256, 16, 1),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_7[
grid(768)](buf20, buf26, primals_12, primals_13, primals_11,
buf21, buf22, buf23, buf27, 768, 256, num_warps=2, num_stages=1)
del primals_11
del primals_12
del primals_13
buf28 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(248832)](buf27, buf28,
248832, XBLOCK=1024, num_warps=4, num_stages=1)
buf29 = extern_kernels.convolution(buf28, primals_14, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf29, (4, 192, 16, 16), (49152, 256, 16, 1))
buf30 = buf29
del buf29
buf33 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 1, 1), torch.
float32)
buf34 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf36 = reinterpret_tensor(buf34, (1, 768, 1, 1), (768, 1, 1, 1), 0)
del buf34
triton_per_fused__native_batch_norm_legit_convolution_9[grid(768)](
buf30, buf36, primals_15, buf33, 768, 256, num_warps=2,
num_stages=1)
del primals_15
buf31 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_16, buf31, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_16
buf32 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_17, buf32, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_17
buf37 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(248832)](buf30,
buf33, buf36, buf31, buf32, buf37, 248832, XBLOCK=512,
num_warps=8, num_stages=1)
buf38 = extern_kernels.convolution(buf37, primals_18, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf38, (4, 192, 16, 16), (49152, 256, 16, 1))
buf40 = empty_strided_cuda((768,), (1,), torch.float32)
buf39 = buf38
del buf38
buf41 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf45 = buf27
del buf27
buf44 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(768)](buf39, buf45, primals_20, primals_19, primals_21,
buf40, buf41, buf44, 768, 256, num_warps=2, num_stages=1)
del primals_19
del primals_20
del primals_21
buf46 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(248832)](buf45, buf46,
248832, XBLOCK=1024, num_warps=4, num_stages=1)
buf47 = extern_kernels.convolution(buf46, primals_22, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf47, (4, 192, 16, 16), (49152, 256, 16, 1))
buf48 = buf47
del buf47
buf51 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 1, 1), torch.
float32)
buf52 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf54 = reinterpret_tensor(buf52, (1, 768, 1, 1), (768, 1, 1, 1), 0)
del buf52
triton_per_fused__native_batch_norm_legit_convolution_9[grid(768)](
buf48, buf54, primals_23, buf51, 768, 256, num_warps=2,
num_stages=1)
del primals_23
buf49 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_24, buf49, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_24
buf50 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_25, buf50, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_25
buf55 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(248832)](buf48,
buf51, buf54, buf49, buf50, buf55, 248832, XBLOCK=512,
num_warps=8, num_stages=1)
buf56 = extern_kernels.convolution(buf55, primals_26, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf56, (4, 192, 16, 16), (49152, 256, 16, 1))
buf58 = empty_strided_cuda((768,), (1,), torch.float32)
buf57 = buf56
del buf56
buf59 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf63 = buf45
del buf45
buf62 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(768)](buf57, buf63, primals_28, primals_27, primals_29,
buf58, buf59, buf62, 768, 256, num_warps=2, num_stages=1)
del primals_27
del primals_28
del primals_29
buf64 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(248832)](buf63, buf64,
248832, XBLOCK=1024, num_warps=4, num_stages=1)
buf65 = extern_kernels.convolution(buf64, primals_30, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf65, (4, 192, 16, 16), (49152, 256, 16, 1))
buf66 = buf65
del buf65
buf69 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 1, 1), torch.
float32)
buf70 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf72 = reinterpret_tensor(buf70, (1, 768, 1, 1), (768, 1, 1, 1), 0)
del buf70
triton_per_fused__native_batch_norm_legit_convolution_9[grid(768)](
buf66, buf72, primals_31, buf69, 768, 256, num_warps=2,
num_stages=1)
del primals_31
buf67 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_32, buf67, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_32
buf68 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_33, buf68, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_33
buf73 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(248832)](buf66,
buf69, buf72, buf67, buf68, buf73, 248832, XBLOCK=512,
num_warps=8, num_stages=1)
buf74 = extern_kernels.convolution(buf73, primals_34, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf74, (4, 192, 16, 16), (49152, 256, 16, 1))
buf76 = empty_strided_cuda((768,), (1,), torch.float32)
buf75 = buf74
del buf74
buf77 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf81 = buf63
del buf63
buf80 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_12[
grid(768)](buf75, buf81, primals_36, primals_35, primals_37,
buf76, buf77, buf80, 768, 256, num_warps=2, num_stages=1)
del primals_35
del primals_36
del primals_37
buf82 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_8[grid(248832)](buf81, buf82,
248832, XBLOCK=1024, num_warps=4, num_stages=1)
buf83 = extern_kernels.convolution(buf82, primals_38, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf83, (4, 192, 16, 16), (49152, 256, 16, 1))
buf84 = buf83
del buf83
buf87 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 1, 1), torch.
float32)
buf88 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf90 = reinterpret_tensor(buf88, (1, 768, 1, 1), (768, 1, 1, 1), 0)
del buf88
triton_per_fused__native_batch_norm_legit_convolution_9[grid(768)](
buf84, buf90, primals_39, buf87, 768, 256, num_warps=2,
num_stages=1)
del primals_39
buf85 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_40, buf85, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_40
buf86 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_41, buf86, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_41
buf91 = empty_strided_cuda((4, 192, 18, 18), (62208, 324, 18, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_11[grid(248832)](buf84,
buf87, buf90, buf85, buf86, buf91, 248832, XBLOCK=512,
num_warps=8, num_stages=1)
buf92 = extern_kernels.convolution(buf91, primals_42, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf92, (4, 192, 16, 16), (49152, 256, 16, 1))
buf93 = buf92
del buf92
buf95 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf96 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
buf98 = empty_strided_cuda((1, 768, 1, 1), (768, 1, 768, 768),
torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_13[grid(768)](
buf93, primals_43, buf95, buf96, buf98, 768, 256, num_warps=2,
num_stages=1)
del primals_43
buf94 = empty_strided_cuda((768,), (1,), torch.float32)
triton_poi_fused_repeat_10[grid(768)](primals_44, buf94, 768,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_44
buf99 = empty_strided_cuda((32,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_14[grid(32)](buf99, 32,
XBLOCK=32, num_warps=1, num_stages=1)
buf100 = empty_strided_cuda((4, 192, 34, 34), (221952, 1156, 34, 1),
torch.float32)
triton_poi_fused__unsafe_index_add_reflection_pad2d_15[grid(887808)](
buf99, buf93, buf95, buf96, buf94, primals_45, buf81, buf100,
887808, XBLOCK=512, num_warps=8, num_stages=1)
del buf81
del buf96
del primals_45
buf101 = extern_kernels.convolution(buf100, primals_46, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf101, (4, 96, 32, 32), (98304, 1024, 32, 1))
buf102 = buf101
del buf101
buf105 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 1, 1), torch.
float32)
buf106 = empty_strided_cuda((1, 384, 1, 1), (384, 1, 384, 384),
torch.float32)
buf108 = reinterpret_tensor(buf106, (1, 384, 1, 1), (384, 1, 1, 1), 0)
del buf106
triton_per_fused__native_batch_norm_legit_convolution_4[grid(384)](
buf102, buf108, primals_47, buf105, 384, 1024, num_warps=8,
num_stages=1)
del primals_47
buf103 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(384)](primals_48, buf103, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_48
buf104 = empty_strided_cuda((384,), (1,), torch.float32)
triton_poi_fused_repeat_5[grid(384)](primals_49, buf104, 384,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_49
buf109 = empty_strided_cuda((64,), (1,), torch.int64)
triton_poi_fused__to_copy_add_arange_mul_16[grid(64)](buf109, 64,
XBLOCK=64, num_warps=1, num_stages=1)
buf110 = empty_strided_cuda((4, 96, 66, 66), (418176, 4356, 66, 1),
torch.float32)
triton_poi_fused__unsafe_index_reflection_pad2d_relu_17[grid(1672704)](
buf109, buf102, buf105, buf108, buf103, buf104, buf110, 1672704,
XBLOCK=1024, num_warps=4, num_stages=1)
buf111 = extern_kernels.convolution(buf110, primals_50, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf111, (4, 48, 64, 64), (196608, 4096, 64, 1))
buf112 = buf111
del buf111
buf115 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 1, 1), torch.
float32)
buf116 = empty_strided_cuda((1, 192, 1, 1), (192, 1, 192, 192),
torch.float32)
buf118 = reinterpret_tensor(buf116, (1, 192, 1, 1), (192, 1, 1, 1), 0)
del buf116
triton_red_fused__native_batch_norm_legit_convolution_1[grid(192)](
buf112, buf118, primals_51, buf115, 192, 4096, XBLOCK=1, RBLOCK
=2048, num_warps=16, num_stages=1)
del primals_51
buf113 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(192)](primals_52, buf113, 192,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_52
buf114 = empty_strided_cuda((192,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(192)](primals_53, buf114, 192,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_53
buf119 = empty_strided_cuda((4, 48, 72, 72), (248832, 5184, 72, 1),
torch.float32)
triton_poi_fused_reflection_pad2d_relu_18[grid(995328)](buf112,
buf115, buf118, buf113, buf114, buf119, 995328, XBLOCK=1024,
num_warps=4, num_stages=1)
buf120 = extern_kernels.convolution(buf119, primals_54, stride=(1,
1), padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf120, (4, 3, 64, 64), (12288, 4096, 64, 1))
buf121 = buf120
del buf120
buf122 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1),
torch.float32)
triton_poi_fused_add_convolution_div_mul_tanh_19[grid(49152)](buf121,
primals_55, buf122, 49152, XBLOCK=256, num_warps=4, num_stages=1)
del primals_55
return (buf122, primals_2, primals_6, primals_10, primals_14,
primals_18, primals_22, primals_26, primals_30, primals_34,
primals_38, primals_42, primals_46, primals_50, primals_54, buf0,
buf2, buf3, buf4, buf5, buf8, buf9, buf11, buf12, buf13, buf14,
buf17, buf18, buf20, buf21, buf22, buf23, buf26, buf28, buf30,
buf31, buf32, buf33, buf36, buf37, buf39, buf40, reinterpret_tensor
(buf44, (768,), (1,), 0), buf46, buf48, buf49, buf50, buf51, buf54,
buf55, buf57, buf58, reinterpret_tensor(buf62, (768,), (1,), 0),
buf64, buf66, buf67, buf68, buf69, buf72, buf73, buf75, buf76,
reinterpret_tensor(buf80, (768,), (1,), 0), buf82, buf84, buf85,
buf86, buf87, buf90, buf91, buf93, buf94, reinterpret_tensor(buf98,
(768,), (1,), 0), buf99, buf100, buf102, buf103, buf104, buf105,
buf108, buf109, buf110, buf112, buf113, buf114, buf115, buf118,
buf119, buf121, reinterpret_tensor(buf95, (1, 768, 1, 1), (768, 1,
1, 1), 0), reinterpret_tensor(buf77, (1, 768, 1, 1), (768, 1, 1, 1),
0), reinterpret_tensor(buf59, (1, 768, 1, 1), (768, 1, 1, 1), 0),
reinterpret_tensor(buf41, (1, 768, 1, 1), (768, 1, 1, 1), 0))
class SelectiveLoadModule(torch.nn.Module):
"""Only load layers in trained models with the same name."""
def __init__(self):
super(SelectiveLoadModule, self).__init__()
def forward(self, x):
return x
def load_state_dict(self, state_dict):
"""Override the function to ignore redundant weights."""
own_state = self.state_dict()
for name, param in state_dict.items():
if name in own_state:
own_state[name].copy_(param)
class ConvLayer(torch.nn.Module):
"""Reflection padded convolution layer."""
def __init__(self, in_channels, out_channels, kernel_size, stride, bias
=True):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride=stride, bias=bias)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ConvTanh(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvTanh, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.tanh = torch.nn.Tanh()
def forward(self, x):
out = super(ConvTanh, self).forward(x)
return self.tanh(out / 255) * 150 + 255 / 2
class ConvInstRelu(ConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvInstRelu, self).__init__(in_channels, out_channels,
kernel_size, stride)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(ConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class UpsampleConvLayer(torch.nn.Module):
"""Upsamples the input and then does a convolution.
This method gives better results compared to ConvTranspose2d.
ref: http://distill.pub/2016/deconv-checkerboard/
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding)
self.conv2d = torch.nn.Conv2d(in_channels, out_channels,
kernel_size, stride)
def forward(self, x):
x_in = x
if self.upsample:
x_in = self.upsample_layer(x_in)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
class UpsampleConvInstRelu(UpsampleConvLayer):
def __init__(self, in_channels, out_channels, kernel_size, stride,
upsample=None):
super(UpsampleConvInstRelu, self).__init__(in_channels,
out_channels, kernel_size, stride, upsample)
self.instance = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
out = super(UpsampleConvInstRelu, self).forward(x)
out = self.instance(out)
out = self.relu(out)
return out
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, out_channels, kernel_size, stride)
self.in1 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.conv2 = ConvLayer(out_channels, out_channels, kernel_size, stride)
self.in2 = torch.nn.InstanceNorm2d(out_channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
return out
class ReCoNet2New(SelectiveLoadModule):
def __init__(self):
super(ReCoNet2New, self).__init__()
self.style_conv1 = ConvInstRelu(3, 48, kernel_size=9, stride=1)
self.style_conv2 = ConvInstRelu(48, 96, kernel_size=3, stride=2)
self.style_conv3 = ConvInstRelu(96, 192, kernel_size=3, stride=2)
self.style_res1 = ResidualBlock(192, 192)
self.style_res2 = ResidualBlock(192, 192)
self.style_res3 = ResidualBlock(192, 192)
self.style_res4 = ResidualBlock(192, 192)
self.style_deconv1 = UpsampleConvInstRelu(192, 96, kernel_size=3,
stride=1, upsample=2)
self.style_deconv2 = UpsampleConvInstRelu(96, 48, kernel_size=3,
stride=1, upsample=2)
self.style_deconv3 = ConvTanh(48, 3, kernel_size=9, stride=1)
def forward(self, input_0):
primals_2 = self.style_conv1.conv2d.weight
primals_3 = self.style_conv1.conv2d.bias
primals_4 = self.style_conv1.instance.weight
primals_5 = self.style_conv1.instance.bias
primals_6 = self.style_conv2.conv2d.weight
primals_7 = self.style_conv2.conv2d.bias
primals_8 = self.style_conv2.instance.weight
primals_9 = self.style_conv2.instance.bias
primals_10 = self.style_conv3.conv2d.weight
primals_11 = self.style_conv3.conv2d.bias
primals_12 = self.style_conv3.instance.weight
primals_13 = self.style_conv3.instance.bias
primals_14 = self.style_res1.conv1.conv2d.weight
primals_15 = self.style_res1.conv1.conv2d.bias
primals_16 = self.style_res1.in1.weight
primals_17 = self.style_res1.in1.bias
primals_18 = self.style_res1.conv2.conv2d.weight
primals_19 = self.style_res1.conv2.conv2d.bias
primals_20 = self.style_res1.in2.weight
primals_21 = self.style_res1.in2.bias
primals_22 = self.style_res2.conv1.conv2d.weight
primals_23 = self.style_res2.conv1.conv2d.bias
primals_24 = self.style_res2.in1.weight
primals_25 = self.style_res2.in1.bias
primals_26 = self.style_res2.conv2.conv2d.weight
primals_27 = self.style_res2.conv2.conv2d.bias
primals_28 = self.style_res2.in2.weight
primals_29 = self.style_res2.in2.bias
primals_30 = self.style_res3.conv1.conv2d.weight
primals_31 = self.style_res3.conv1.conv2d.bias
primals_32 = self.style_res3.in1.weight
primals_33 = self.style_res3.in1.bias
primals_34 = self.style_res3.conv2.conv2d.weight
primals_35 = self.style_res3.conv2.conv2d.bias
primals_36 = self.style_res3.in2.weight
primals_37 = self.style_res3.in2.bias
primals_38 = self.style_res4.conv1.conv2d.weight
primals_39 = self.style_res4.conv1.conv2d.bias
primals_40 = self.style_res4.in1.weight
primals_41 = self.style_res4.in1.bias
primals_42 = self.style_res4.conv2.conv2d.weight
primals_43 = self.style_res4.conv2.conv2d.bias
primals_44 = self.style_res4.in2.weight
primals_45 = self.style_res4.in2.bias
primals_46 = self.style_deconv1.conv2d.weight
primals_47 = self.style_deconv1.conv2d.bias
primals_48 = self.style_deconv1.instance.weight
primals_49 = self.style_deconv1.instance.bias
primals_50 = self.style_deconv2.conv2d.weight
primals_51 = self.style_deconv2.conv2d.bias
primals_52 = self.style_deconv2.instance.weight
primals_53 = self.style_deconv2.instance.bias
primals_54 = self.style_deconv3.conv2d.weight
primals_55 = self.style_deconv3.conv2d.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20, primals_21, primals_22, primals_23, primals_24,
primals_25, primals_26, primals_27, primals_28, primals_29,
primals_30, primals_31, primals_32, primals_33, primals_34,
primals_35, primals_36, primals_37, primals_38, primals_39,
primals_40, primals_41, primals_42, primals_43, primals_44,
primals_45, primals_46, primals_47, primals_48, primals_49,
primals_50, primals_51, primals_52, primals_53, primals_54,
primals_55])
return output[0]
|
irsisyphus/reconet
|
ReCoNet2
| false
| 15,690
|
[
"MIT"
] | 56
|
863acf8dde4d45c8521634af27878fe04f3b2e56
|
https://github.com/irsisyphus/reconet/tree/863acf8dde4d45c8521634af27878fe04f3b2e56
|
Normalize
|
import torch
import torch.nn as nn
class Normalize(nn.Module):
def __init__(self, features, epsilon=1e-06):
super(Normalize, self).__init__()
self.gain = nn.Parameter(torch.ones(features))
self.bias = nn.Parameter(torch.zeros(features))
self.epsilon = epsilon
def forward(self, x, dim=-1):
mu = x.mean(dim, keepdim=True)
sigma = torch.sqrt(x.var(dim, keepdim=True) + self.epsilon)
gain = self.gain
bias = self.bias
if dim != -1:
shape = [1] * len(mu.size())
shape[dim] = self.gain.size()[0]
gain = gain.view(shape)
bias = bias.view(shape)
return gain * (x - mu) / (sigma + self.epsilon) + bias
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'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
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_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
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp31 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 4.0
tmp10 = tmp8 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = tmp0 * tmp11
tmp13 = tmp2 - tmp10
tmp14 = tmp13 * tmp13
tmp15 = tmp3 - tmp10
tmp16 = tmp15 * tmp15
tmp17 = tmp14 + tmp16
tmp18 = tmp5 - tmp10
tmp19 = tmp18 * tmp18
tmp20 = tmp17 + tmp19
tmp21 = tmp7 - tmp10
tmp22 = tmp21 * tmp21
tmp23 = tmp20 + tmp22
tmp24 = 3.0
tmp25 = tmp23 / tmp24
tmp26 = 1e-06
tmp27 = tmp25 + tmp26
tmp28 = libdevice.sqrt(tmp27)
tmp29 = tmp28 + tmp26
tmp30 = tmp12 / tmp29
tmp32 = tmp30 + tmp31
tl.store(in_out_ptr0 + x2, tmp32, 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)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_add_div_mean_mul_sqrt_sub_var_0[grid(256)](buf1,
primals_2, primals_1, primals_3, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del primals_2
del primals_3
return buf1, primals_1
class NormalizeNew(nn.Module):
def __init__(self, features, epsilon=1e-06):
super(NormalizeNew, self).__init__()
self.gain = nn.Parameter(torch.ones(features))
self.bias = nn.Parameter(torch.zeros(features))
self.epsilon = epsilon
def forward(self, input_0):
primals_2 = self.gain
primals_3 = self.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
jingraham/struct2seq
|
Normalize
| false
| 15,691
|
[
"MIT"
] | 106
|
22e497a2b565fe82f17e12ea37e89dcf4e50e92f
|
https://github.com/jingraham/struct2seq/tree/22e497a2b565fe82f17e12ea37e89dcf4e50e92f
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.