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
|
|---|---|---|---|---|---|---|---|---|---|---|
SuperPointNet
|
import torch
import torch.optim
import torch.utils.data
class SuperPointNet(torch.nn.Module):
""" Pytorch definition of SuperPoint Network. """
def __init__(self):
super(SuperPointNet, self).__init__()
self.relu = torch.nn.ReLU(inplace=True)
self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2)
c1, c2, c3, c4, c5, d1 = 64, 64, 128, 128, 256, 256
self.conv1a = torch.nn.Conv2d(1, c1, kernel_size=3, stride=1, padding=1
)
self.conv1b = torch.nn.Conv2d(c1, c1, kernel_size=3, stride=1,
padding=1)
self.conv2a = torch.nn.Conv2d(c1, c2, kernel_size=3, stride=1,
padding=1)
self.conv2b = torch.nn.Conv2d(c2, c2, kernel_size=3, stride=1,
padding=1)
self.conv3a = torch.nn.Conv2d(c2, c3, kernel_size=3, stride=1,
padding=1)
self.conv3b = torch.nn.Conv2d(c3, c3, kernel_size=3, stride=1,
padding=1)
self.conv4a = torch.nn.Conv2d(c3, c4, kernel_size=3, stride=1,
padding=1)
self.conv4b = torch.nn.Conv2d(c4, c4, kernel_size=3, stride=1,
padding=1)
self.convPa = torch.nn.Conv2d(c4, c5, kernel_size=3, stride=1,
padding=1)
self.convPb = torch.nn.Conv2d(c5, 65, kernel_size=1, stride=1,
padding=0)
self.convDa = torch.nn.Conv2d(c4, c5, kernel_size=3, stride=1,
padding=1)
self.convDb = torch.nn.Conv2d(c5, d1, kernel_size=1, stride=1,
padding=0)
def forward(self, x):
""" Forward pass that jointly computes unprocessed point and descriptor
tensors.
Input
x: Image pytorch tensor shaped N x 1 x H x W.
Output
semi: Output point pytorch tensor shaped N x 65 x H/8 x W/8.
desc: Output descriptor pytorch tensor shaped N x 256 x H/8 x W/8.
"""
x = self.relu(self.conv1a(x))
x = self.relu(self.conv1b(x))
x = self.pool(x)
x = self.relu(self.conv2a(x))
x = self.relu(self.conv2b(x))
x = self.pool(x)
x = self.relu(self.conv3a(x))
x = self.relu(self.conv3b(x))
x = self.pool(x)
x = self.relu(self.conv4a(x))
x = self.relu(self.conv4b(x))
cPa = self.relu(self.convPa(x))
semi = self.convPb(cPa)
cDa = self.relu(self.convDa(x))
desc = self.convDb(cDa)
dn = torch.norm(desc, p=2, dim=1)
desc = desc.div(torch.unsqueeze(dn, 1))
return semi, desc
def get_inputs():
return [torch.rand([4, 1, 64, 64])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import torch.optim
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 64
y1 = yindex // 64
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 64
y1 = yindex // 64
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 128
y1 = yindex // 128
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 128
y1 = yindex // 128
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_convolution_relu_4(in_ptr0, in_ptr1, out_ptr0, ynumel,
xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 256
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
x2 = xindex
y3 = yindex
y0 = yindex % 64
y1 = yindex // 64
tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1, 1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(out_ptr0 + (y0 + 64 * x2 + 262144 * y1), tmp4, ymask)
@triton.jit
def triton_poi_fused_convolution_relu_5(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 64
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_max_pool2d_with_indices_6(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 64
x1 = xindex // 64 % 32
x2 = xindex // 2048
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 128 * x1 + 8192 * x2), None)
tmp1 = tl.load(in_ptr0 + (64 + x0 + 128 * x1 + 8192 * x2), None)
tmp3 = tl.load(in_ptr0 + (4096 + x0 + 128 * x1 + 8192 * x2), None)
tmp5 = tl.load(in_ptr0 + (4160 + x0 + 128 * x1 + 8192 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_relu_7(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 64
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_max_pool2d_with_indices_8(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 64
x1 = xindex // 64 % 16
x2 = xindex // 1024
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 128 * x1 + 4096 * x2), None)
tmp1 = tl.load(in_ptr0 + (64 + x0 + 128 * x1 + 4096 * x2), None)
tmp3 = tl.load(in_ptr0 + (2048 + x0 + 128 * x1 + 4096 * x2), None)
tmp5 = tl.load(in_ptr0 + (2112 + x0 + 128 * x1 + 4096 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_relu_9(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_max_pool2d_with_indices_10(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 128
x1 = xindex // 128 % 8
x2 = xindex // 1024
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 256 * x1 + 4096 * x2), None)
tmp1 = tl.load(in_ptr0 + (128 + x0 + 256 * x1 + 4096 * x2), None)
tmp3 = tl.load(in_ptr0 + (2048 + x0 + 256 * x1 + 4096 * x2), None)
tmp5 = tl.load(in_ptr0 + (2176 + x0 + 256 * x1 + 4096 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_relu_11(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_convolution_relu_12(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 256
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_convolution_13(in_ptr0, in_ptr1, out_ptr0, ynumel,
xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
ynumel = 260
xnumel = 64
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 % 65
y1 = yindex // 65
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 65 * x2 + 4160 * 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 + 64 * y3), tmp2, xmask & ymask)
@triton.jit
def triton_per_fused_convolution_linalg_vector_norm_14(in_out_ptr0,
in_out_ptr1, in_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)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_out_ptr0 + (r1 + 256 * x0), None)
tmp1 = tl.load(in_ptr0 + r1, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tmp2 * tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tmp7 = libdevice.sqrt(tmp6)
tl.store(in_out_ptr0 + (r1 + 256 * x0), tmp2, None)
tl.debug_barrier()
tl.store(in_out_ptr1 + x0, tmp7, None)
@triton.jit
def triton_poi_fused_div_15(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
xnumel = 64
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 256
y1 = yindex // 256
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 16384 * y1), xmask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + (x2 + 64 * y1), xmask, eviction_policy=
'evict_last')
tmp2 = tmp0 / tmp1
tl.store(out_ptr0 + (x2 + 64 * y3), tmp2, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, 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) = args
args.clear()
assert_size_stride(primals_1, (64, 1, 3, 3), (9, 9, 3, 1))
assert_size_stride(primals_2, (64,), (1,))
assert_size_stride(primals_3, (4, 1, 64, 64), (4096, 4096, 64, 1))
assert_size_stride(primals_4, (64, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_5, (64,), (1,))
assert_size_stride(primals_6, (64, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_7, (64,), (1,))
assert_size_stride(primals_8, (64, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_9, (64,), (1,))
assert_size_stride(primals_10, (128, 64, 3, 3), (576, 9, 3, 1))
assert_size_stride(primals_11, (128,), (1,))
assert_size_stride(primals_12, (128, 128, 3, 3), (1152, 9, 3, 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, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_17, (128,), (1,))
assert_size_stride(primals_18, (256, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_19, (256,), (1,))
assert_size_stride(primals_20, (65, 256, 1, 1), (256, 1, 1, 1))
assert_size_stride(primals_21, (65,), (1,))
assert_size_stride(primals_22, (256, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_23, (256,), (1,))
assert_size_stride(primals_24, (256, 256, 1, 1), (256, 1, 1, 1))
assert_size_stride(primals_25, (256,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch.
float32)
get_raw_stream(0)
triton_poi_fused_0[grid(4096, 9)](primals_4, buf0, 4096, 9, XBLOCK=
16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_4
buf1 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch.
float32)
triton_poi_fused_0[grid(4096, 9)](primals_6, buf1, 4096, 9, XBLOCK=
16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_6
buf2 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch.
float32)
triton_poi_fused_0[grid(4096, 9)](primals_8, buf2, 4096, 9, XBLOCK=
16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_8
buf3 = empty_strided_cuda((128, 64, 3, 3), (576, 1, 192, 64), torch
.float32)
triton_poi_fused_1[grid(8192, 9)](primals_10, buf3, 8192, 9, XBLOCK
=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_10
buf4 = empty_strided_cuda((128, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_2[grid(16384, 9)](primals_12, buf4, 16384, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_12
buf5 = empty_strided_cuda((128, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_2[grid(16384, 9)](primals_14, buf5, 16384, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_14
buf6 = empty_strided_cuda((128, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_2[grid(16384, 9)](primals_16, buf6, 16384, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_16
buf7 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_3[grid(32768, 9)](primals_18, buf7, 32768, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_18
buf8 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_3[grid(32768, 9)](primals_22, buf8, 32768, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_22
buf9 = 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(buf9, (4, 64, 64, 64), (262144, 4096, 64, 1))
buf10 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64),
torch.float32)
triton_poi_fused_convolution_relu_4[grid(256, 4096)](buf9,
primals_2, buf10, 256, 4096, XBLOCK=32, YBLOCK=32, num_warps=4,
num_stages=1)
del buf9
del primals_2
buf11 = extern_kernels.convolution(buf10, buf0, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf11, (4, 64, 64, 64), (262144, 1, 4096, 64))
buf12 = buf11
del buf11
triton_poi_fused_convolution_relu_5[grid(1048576)](buf12, primals_5,
1048576, XBLOCK=512, num_warps=8, num_stages=1)
del primals_5
buf13 = empty_strided_cuda((4, 64, 32, 32), (65536, 1, 2048, 64),
torch.float32)
buf14 = empty_strided_cuda((4, 64, 32, 32), (65536, 1, 2048, 64),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_6[grid(262144)](buf12,
buf13, buf14, 262144, XBLOCK=512, num_warps=8, num_stages=1)
buf15 = extern_kernels.convolution(buf13, buf1, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf15, (4, 64, 32, 32), (65536, 1, 2048, 64))
buf16 = buf15
del buf15
triton_poi_fused_convolution_relu_7[grid(262144)](buf16, primals_7,
262144, XBLOCK=512, num_warps=8, num_stages=1)
del primals_7
buf17 = extern_kernels.convolution(buf16, buf2, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf17, (4, 64, 32, 32), (65536, 1, 2048, 64))
buf18 = buf17
del buf17
triton_poi_fused_convolution_relu_7[grid(262144)](buf18, primals_9,
262144, XBLOCK=512, num_warps=8, num_stages=1)
del primals_9
buf19 = empty_strided_cuda((4, 64, 16, 16), (16384, 1, 1024, 64),
torch.float32)
buf20 = empty_strided_cuda((4, 64, 16, 16), (16384, 1, 1024, 64),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_8[grid(65536)](buf18,
buf19, buf20, 65536, XBLOCK=256, num_warps=4, num_stages=1)
buf21 = extern_kernels.convolution(buf19, buf3, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf21, (4, 128, 16, 16), (32768, 1, 2048, 128))
buf22 = buf21
del buf21
triton_poi_fused_convolution_relu_9[grid(131072)](buf22, primals_11,
131072, XBLOCK=512, num_warps=8, num_stages=1)
del primals_11
buf23 = extern_kernels.convolution(buf22, buf4, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf23, (4, 128, 16, 16), (32768, 1, 2048, 128))
buf24 = buf23
del buf23
triton_poi_fused_convolution_relu_9[grid(131072)](buf24, primals_13,
131072, XBLOCK=512, num_warps=8, num_stages=1)
del primals_13
buf25 = empty_strided_cuda((4, 128, 8, 8), (8192, 1, 1024, 128),
torch.float32)
buf26 = empty_strided_cuda((4, 128, 8, 8), (8192, 1, 1024, 128),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_10[grid(32768)](buf24,
buf25, buf26, 32768, XBLOCK=128, num_warps=4, num_stages=1)
buf27 = extern_kernels.convolution(buf25, buf5, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf27, (4, 128, 8, 8), (8192, 1, 1024, 128))
buf28 = buf27
del buf27
triton_poi_fused_convolution_relu_11[grid(32768)](buf28, primals_15,
32768, XBLOCK=256, num_warps=4, num_stages=1)
del primals_15
buf29 = extern_kernels.convolution(buf28, buf6, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf29, (4, 128, 8, 8), (8192, 1, 1024, 128))
buf30 = buf29
del buf29
triton_poi_fused_convolution_relu_11[grid(32768)](buf30, primals_17,
32768, XBLOCK=256, num_warps=4, num_stages=1)
del primals_17
buf31 = extern_kernels.convolution(buf30, buf7, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf31, (4, 256, 8, 8), (16384, 1, 2048, 256))
buf32 = buf31
del buf31
triton_poi_fused_convolution_relu_12[grid(65536)](buf32, primals_19,
65536, XBLOCK=256, num_warps=4, num_stages=1)
del primals_19
buf33 = extern_kernels.convolution(buf32, primals_20, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf33, (4, 65, 8, 8), (4160, 1, 520, 65))
buf34 = empty_strided_cuda((4, 65, 8, 8), (4160, 64, 8, 1), torch.
float32)
triton_poi_fused_convolution_13[grid(260, 64)](buf33, primals_21,
buf34, 260, 64, XBLOCK=64, YBLOCK=4, num_warps=4, num_stages=1)
del buf33
del primals_21
buf35 = extern_kernels.convolution(buf30, buf8, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf35, (4, 256, 8, 8), (16384, 1, 2048, 256))
buf36 = buf35
del buf35
triton_poi_fused_convolution_relu_12[grid(65536)](buf36, primals_23,
65536, XBLOCK=256, num_warps=4, num_stages=1)
del primals_23
buf37 = extern_kernels.convolution(buf36, primals_24, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf37, (4, 256, 8, 8), (16384, 1, 2048, 256))
buf38 = buf37
del buf37
buf39 = empty_strided_cuda((4, 8, 8), (64, 8, 1), torch.float32)
buf40 = buf39
del buf39
triton_per_fused_convolution_linalg_vector_norm_14[grid(256)](buf38,
buf40, primals_25, 256, 256, num_warps=2, num_stages=1)
del primals_25
buf41 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch
.float32)
triton_poi_fused_div_15[grid(1024, 64)](buf38, buf40, buf41, 1024,
64, XBLOCK=64, YBLOCK=8, num_warps=4, num_stages=1)
return (buf34, buf41, primals_1, primals_3, buf0, buf1, buf2, buf3,
buf4, buf5, buf6, buf7, primals_20, buf8, primals_24, buf10, buf12,
buf13, buf14, buf16, buf18, buf19, buf20, buf22, buf24, buf25,
buf26, buf28, buf30, buf32, buf36, buf38, reinterpret_tensor(buf40,
(4, 1, 8, 8), (64, 64, 8, 1), 0))
class SuperPointNetNew(torch.nn.Module):
""" Pytorch definition of SuperPoint Network. """
def __init__(self):
super(SuperPointNetNew, self).__init__()
self.relu = torch.nn.ReLU(inplace=True)
self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2)
c1, c2, c3, c4, c5, d1 = 64, 64, 128, 128, 256, 256
self.conv1a = torch.nn.Conv2d(1, c1, kernel_size=3, stride=1, padding=1
)
self.conv1b = torch.nn.Conv2d(c1, c1, kernel_size=3, stride=1,
padding=1)
self.conv2a = torch.nn.Conv2d(c1, c2, kernel_size=3, stride=1,
padding=1)
self.conv2b = torch.nn.Conv2d(c2, c2, kernel_size=3, stride=1,
padding=1)
self.conv3a = torch.nn.Conv2d(c2, c3, kernel_size=3, stride=1,
padding=1)
self.conv3b = torch.nn.Conv2d(c3, c3, kernel_size=3, stride=1,
padding=1)
self.conv4a = torch.nn.Conv2d(c3, c4, kernel_size=3, stride=1,
padding=1)
self.conv4b = torch.nn.Conv2d(c4, c4, kernel_size=3, stride=1,
padding=1)
self.convPa = torch.nn.Conv2d(c4, c5, kernel_size=3, stride=1,
padding=1)
self.convPb = torch.nn.Conv2d(c5, 65, kernel_size=1, stride=1,
padding=0)
self.convDa = torch.nn.Conv2d(c4, c5, kernel_size=3, stride=1,
padding=1)
self.convDb = torch.nn.Conv2d(c5, d1, kernel_size=1, stride=1,
padding=0)
def forward(self, input_0):
primals_1 = self.conv1a.weight
primals_2 = self.conv1a.bias
primals_4 = self.conv1b.weight
primals_5 = self.conv1b.bias
primals_6 = self.conv2a.weight
primals_7 = self.conv2a.bias
primals_8 = self.conv2b.weight
primals_9 = self.conv2b.bias
primals_10 = self.conv3a.weight
primals_11 = self.conv3a.bias
primals_12 = self.conv3b.weight
primals_13 = self.conv3b.bias
primals_14 = self.conv4a.weight
primals_15 = self.conv4a.bias
primals_16 = self.conv4b.weight
primals_17 = self.conv4b.bias
primals_18 = self.convPa.weight
primals_19 = self.convPa.bias
primals_20 = self.convPb.weight
primals_21 = self.convPb.bias
primals_22 = self.convDa.weight
primals_23 = self.convDa.bias
primals_24 = self.convDb.weight
primals_25 = self.convDb.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20, primals_21, primals_22, primals_23, primals_24,
primals_25])
return output[0], output[1]
|
Dai-z/pytorch-superpoint
|
SuperPointNet
| false
| 13,572
|
[
"MIT"
] | 390
|
90e71045238fdcce13f9f0d02bdd0e1126145a10
|
https://github.com/Dai-z/pytorch-superpoint/tree/90e71045238fdcce13f9f0d02bdd0e1126145a10
|
Block
|
import torch
import torch.nn as nn
import torch._C
import torch.serialization
class DropPath(nn.Module):
"""Drop paths (Stochastic Depth) per sample (when applied in main path of
residual blocks).
Args:
drop_prob (float): Drop rate for paths of model. Dropout rate has
to be between 0 and 1. Default: 0.
"""
def __init__(self, drop_prob=0.0):
super(DropPath, self).__init__()
self.drop_prob = drop_prob
self.keep_prob = 1 - drop_prob
def forward(self, x):
if self.drop_prob == 0.0 or not self.training:
return x
shape = (x.shape[0],) + (1,) * (x.ndim - 1)
random_tensor = self.keep_prob + torch.rand(shape, dtype=x.dtype,
device=x.device)
random_tensor.floor_()
output = x.div(self.keep_prob) * random_tensor
return output
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None,
act_layer=nn.GELU, drop=0.0):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None,
attn_drop=0.0, proj_drop=0.0):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** -0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, N, C = x.shape
q, k, v = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.
num_heads).permute(2, 0, 3, 1, 4)
attn = q @ k.transpose(-2, -1) * self.scale
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class Block(nn.Module):
def __init__(self, dim, num_heads, mlp_ratio=4.0, qkv_bias=False,
qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=nn
.GELU, norm_layer=nn.LayerNorm):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = Attention(dim, num_heads=num_heads, qkv_bias=qkv_bias,
qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
self.drop_path = DropPath(drop_path
) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim,
act_layer=act_layer, drop=drop)
def forward(self, x):
x = x + self.drop_path(self.attn(self.norm1(x)))
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'dim': 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 libdevice, math as tl_math
import torch.nn as nn
import torch._C
import torch.serialization
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
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_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 + 12 * x2 + 48 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@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 + (4 + y0 + 12 * x2 + 48 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
)
tmp1 = 1.0
tmp2 = tmp0 * tmp1
tmp4 = tmp3 * tmp1
tmp6 = tmp5 * tmp1
tmp7 = triton_helpers.maximum(tmp4, tmp6)
tmp9 = tmp8 * tmp1
tmp10 = triton_helpers.maximum(tmp7, tmp9)
tmp12 = tmp11 * tmp1
tmp13 = triton_helpers.maximum(tmp10, tmp12)
tmp14 = tmp2 - tmp13
tmp15 = tmp14 * tmp1
tmp16 = tl_math.exp(tmp15)
tl.store(out_ptr0 + x2, tmp16, xmask)
@triton.jit
def triton_poi_fused__softmax_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
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_clone_6(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 + (8 + y0 + 12 * x2 + 48 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_clone_7(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 16
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 4
y1 = yindex // 4
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
eviction_policy='evict_last')
tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_add_native_layer_norm_8(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr2 + 0)
tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr2 + 1)
tmp9 = tl.broadcast_to(tmp8, [XBLOCK])
tmp13 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp14 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp15 = tl.load(in_ptr2 + 2)
tmp16 = tl.broadcast_to(tmp15, [XBLOCK])
tmp20 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp21 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp22 = tl.load(in_ptr2 + 3)
tmp23 = tl.broadcast_to(tmp22, [XBLOCK])
tmp4 = tmp1 + tmp3
tmp5 = tmp0 + tmp4
tmp10 = tmp7 + tmp9
tmp11 = tmp6 + tmp10
tmp12 = tmp5 + tmp11
tmp17 = tmp14 + tmp16
tmp18 = tmp13 + tmp17
tmp19 = tmp12 + tmp18
tmp24 = tmp21 + tmp23
tmp25 = tmp20 + tmp24
tmp26 = tmp19 + tmp25
tmp27 = 4.0
tmp28 = tmp26 / tmp27
tmp29 = tmp5 - tmp28
tmp30 = tmp29 * tmp29
tmp31 = tmp11 - tmp28
tmp32 = tmp31 * tmp31
tmp33 = tmp30 + tmp32
tmp34 = tmp18 - tmp28
tmp35 = tmp34 * tmp34
tmp36 = tmp33 + tmp35
tmp37 = tmp25 - tmp28
tmp38 = tmp37 * tmp37
tmp39 = tmp36 + tmp38
tmp40 = tmp39 / tmp27
tl.store(out_ptr0 + x0, tmp28, xmask)
tl.store(out_ptr1 + x0, tmp40, xmask)
@triton.jit
def triton_poi_fused_add_native_layer_norm_9(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr2 + x0, 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')
tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr6 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tmp6 = tmp4 - tmp5
tmp8 = 1e-05
tmp9 = tmp7 + tmp8
tmp10 = libdevice.rsqrt(tmp9)
tmp11 = tmp6 * tmp10
tmp13 = tmp11 * tmp12
tmp15 = tmp13 + tmp14
tl.store(out_ptr0 + x2, tmp15, xmask)
@triton.jit
def triton_poi_fused_gelu_10(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tmp3 = 0.7071067811865476
tmp4 = tmp0 * tmp3
tmp5 = libdevice.erf(tmp4)
tmp6 = 1.0
tmp7 = tmp5 + tmp6
tmp8 = tmp2 * tmp7
tl.store(out_ptr0 + x0, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_11(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3,
xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_out_ptr0 + x2, xmask)
tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tmp7 = tmp5 + tmp6
tmp8 = tmp4 + tmp7
tl.store(in_out_ptr0 + x2, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12
) = args
args.clear()
assert_size_stride(primals_1, (4,), (1,))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_4, (12, 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,), (1,))
assert_size_stride(primals_8, (4,), (1,))
assert_size_stride(primals_9, (16, 4), (4, 1))
assert_size_stride(primals_10, (16,), (1,))
assert_size_stride(primals_11, (4, 16), (16, 1))
assert_size_stride(primals_12, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
get_raw_stream(0)
triton_poi_fused_native_layer_norm_0[grid(16)](primals_3, buf0,
buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_native_layer_norm_1[grid(64)](primals_3, buf0,
buf1, primals_1, primals_2, buf2, 64, XBLOCK=64, num_warps=1,
num_stages=1)
del primals_1
del primals_2
buf3 = empty_strided_cuda((16, 12), (12, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 12), (1, 4), 0), out=buf3)
buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
triton_poi_fused_clone_2[grid(16, 4)](buf3, buf4, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf5 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 4, 1), torch.float32)
triton_poi_fused_clone_3[grid(16, 4)](buf3, buf5, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf6 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 0),
0), reinterpret_tensor(buf5, (16, 1, 4), (4, 0, 1), 0), out=buf6)
buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_4[grid(256)](buf6, buf7, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf8 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf6
triton_poi_fused__softmax_5[grid(256)](buf7, buf8, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf9 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
triton_poi_fused_clone_6[grid(16, 4)](buf3, buf9, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
del buf3
buf10 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32)
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 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_clone_7[grid(16, 4)](buf10, buf11, 16, 4, XBLOCK=4,
YBLOCK=16, num_warps=1, num_stages=1)
buf12 = reinterpret_tensor(buf10, (16, 4), (4, 1), 0)
del buf10
extern_kernels.mm(reinterpret_tensor(buf11, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf12)
buf13 = buf1
del buf1
buf14 = buf0
del buf0
triton_poi_fused_add_native_layer_norm_8[grid(16)](primals_3, buf12,
primals_6, buf13, buf14, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf15 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_add_native_layer_norm_9[grid(64)](primals_3, buf12,
primals_6, buf13, buf14, primals_7, primals_8, buf15, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del buf13
del buf14
del primals_8
buf16 = reinterpret_tensor(buf7, (16, 16), (16, 1), 0)
del buf7
extern_kernels.addmm(primals_10, reinterpret_tensor(buf15, (16, 4),
(4, 1), 0), reinterpret_tensor(primals_9, (4, 16), (1, 4), 0),
alpha=1, beta=1, out=buf16)
del primals_10
buf17 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
triton_poi_fused_gelu_10[grid(256)](buf16, buf17, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf17, (16, 16), (16, 1), 0),
reinterpret_tensor(primals_11, (16, 4), (1, 16), 0), out=buf18)
buf19 = reinterpret_tensor(buf18, (4, 4, 4), (16, 4, 1), 0)
del buf18
triton_poi_fused_add_11[grid(64)](buf19, primals_3, buf12,
primals_6, primals_12, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_12
return buf19, primals_3, primals_6, primals_7, reinterpret_tensor(buf2,
(16, 4), (4, 1), 0), buf8, reinterpret_tensor(buf11, (16, 4), (4, 1), 0
), buf12, reinterpret_tensor(buf15, (16, 4), (4, 1), 0
), buf16, reinterpret_tensor(buf17, (16, 16), (16, 1), 0
), primals_11, primals_9, primals_5, reinterpret_tensor(buf9, (16,
1, 4), (4, 1, 1), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 1, 1), 0
), reinterpret_tensor(buf5, (16, 4, 1), (4, 1, 4), 0), primals_4
class DropPath(nn.Module):
"""Drop paths (Stochastic Depth) per sample (when applied in main path of
residual blocks).
Args:
drop_prob (float): Drop rate for paths of model. Dropout rate has
to be between 0 and 1. Default: 0.
"""
def __init__(self, drop_prob=0.0):
super(DropPath, self).__init__()
self.drop_prob = drop_prob
self.keep_prob = 1 - drop_prob
def forward(self, x):
if self.drop_prob == 0.0 or not self.training:
return x
shape = (x.shape[0],) + (1,) * (x.ndim - 1)
random_tensor = self.keep_prob + torch.rand(shape, dtype=x.dtype,
device=x.device)
random_tensor.floor_()
output = x.div(self.keep_prob) * random_tensor
return output
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None,
act_layer=nn.GELU, drop=0.0):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None,
attn_drop=0.0, proj_drop=0.0):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** -0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x):
B, N, C = x.shape
q, k, v = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.
num_heads).permute(2, 0, 3, 1, 4)
attn = q @ k.transpose(-2, -1) * self.scale
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B, N, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class BlockNew(nn.Module):
def __init__(self, dim, num_heads, mlp_ratio=4.0, qkv_bias=False,
qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=nn
.GELU, norm_layer=nn.LayerNorm):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = Attention(dim, num_heads=num_heads, qkv_bias=qkv_bias,
qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
self.drop_path = DropPath(drop_path
) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim,
act_layer=act_layer, drop=drop)
def forward(self, input_0):
primals_1 = self.norm1.weight
primals_2 = self.norm1.bias
primals_4 = self.attn.qkv.weight
primals_5 = self.attn.proj.weight
primals_6 = self.attn.proj.bias
primals_7 = self.norm2.weight
primals_8 = self.norm2.bias
primals_9 = self.mlp.fc1.weight
primals_10 = self.mlp.fc1.bias
primals_11 = self.mlp.fc2.weight
primals_12 = self.mlp.fc2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12])
return output[0]
|
CuttlefishXuan/mmsegmentation-1
|
Block
| false
| 13,573
|
[
"Apache-2.0"
] | 789
|
13771312da1a66d5cd642df6aa370affd3f5ceac
|
https://github.com/CuttlefishXuan/mmsegmentation-1/tree/13771312da1a66d5cd642df6aa370affd3f5ceac
|
MeanPoolConv
|
import torch
import torch.nn as nn
class MeanPoolConv(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True):
super().__init__()
self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
def forward(self, inputs):
output = inputs
output = sum([output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]]) / 4.0
return self.conv(output)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 2
x1 = xindex // 2
x2 = xindex
tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy=
'evict_last')
tmp5 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy=
'evict_last')
tmp7 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy=
'evict_last')
tmp1 = 0.0
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tl.store(out_ptr0 + x2, tmp10, 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)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_0[grid(64)](primals_1, buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 2, 2), (16, 4, 2, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(64)](buf2, primals_3, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return buf2, primals_2, buf0
class MeanPoolConvNew(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True):
super().__init__()
self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
def forward(self, input_0):
primals_2 = self.conv.weight
primals_3 = self.conv.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DeepTitan/PNDM
|
MeanPoolConv
| false
| 13,574
|
[
"Apache-2.0"
] | 61
|
4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
https://github.com/DeepTitan/PNDM/tree/4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
_Gate
|
import torch
import torch.nn as nn
class _Gate(nn.Module):
"""Utility class to implement a standard sigmoid gate"""
def __init__(self, in_features: 'int', out_features: 'int'):
super(_Gate, self).__init__()
self.fc = nn.Linear(in_features=in_features, out_features=out_features)
self._reset_parameters()
def _reset_parameters(self):
nn.init.orthogonal_(self.fc.weight)
nn.init.zeros_(self.fc.bias)
def forward(self, x: 'torch.Tensor') ->torch.Tensor:
"""Perform forward pass through the normalised gate"""
return torch.sigmoid(self.fc(x))
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_sigmoid_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 = tl.sigmoid(tmp2)
tl.store(in_out_ptr0 + x2, 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.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_sigmoid_0[grid(256)](buf1, primals_2, 256, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_2
return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1
class _GateNew(nn.Module):
"""Utility class to implement a standard sigmoid gate"""
def __init__(self, in_features: 'int', out_features: 'int'):
super(_GateNew, self).__init__()
self.fc = nn.Linear(in_features=in_features, out_features=out_features)
self._reset_parameters()
def _reset_parameters(self):
nn.init.orthogonal_(self.fc.weight)
nn.init.zeros_(self.fc.bias)
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]
|
DavidChoi76/neuralhydrology
|
_Gate
| false
| 13,575
|
[
"BSD-3-Clause"
] | 144
|
a4c284b92934ee973c8b3fedf8a60df60c8feae1
|
https://github.com/DavidChoi76/neuralhydrology/tree/a4c284b92934ee973c8b3fedf8a60df60c8feae1
|
FSPool
|
import torch
import torch.nn as nn
import torch.utils.data
def deterministic_sort(s, tau):
"""
"Stochastic Optimization of Sorting Networks via Continuous Relaxations" https://openreview.net/forum?id=H1eSS3CcKX
Aditya Grover, Eric Wang, Aaron Zweig, Stefano Ermon
s: input elements to be sorted. Shape: batch_size x n x 1
tau: temperature for relaxation. Scalar.
"""
n = s.size()[1]
one = torch.ones((n, 1), dtype=torch.float32, device=s.device)
A_s = torch.abs(s - s.permute(0, 2, 1))
B = torch.matmul(A_s, torch.matmul(one, one.transpose(0, 1)))
scaling = (n + 1 - 2 * (torch.arange(n, device=s.device) + 1)).type(torch
.float32)
C = torch.matmul(s, scaling.unsqueeze(0))
P_max = (C - B).permute(0, 2, 1)
sm = torch.nn.Softmax(-1)
P_hat = sm(P_max / tau)
return P_hat
def cont_sort(x, perm=None, temp=1):
""" Helper function that calls deterministic_sort with the right shape.
Since it assumes a shape of (batch_size, n, 1) while the input x is of shape (batch_size, channels, n),
we can get this to the right shape by merging the first two dimensions.
If an existing perm is passed in, we compute the "inverse" (transpose of perm) and just use that to unsort x.
"""
original_size = x.size()
x = x.view(-1, x.size(2), 1)
if perm is None:
perm = deterministic_sort(x, temp)
else:
perm = perm.transpose(1, 2)
x = perm.matmul(x)
x = x.view(original_size)
return x, perm
def fill_sizes(sizes, x=None):
"""
sizes is a LongTensor of size [batch_size], containing the set sizes.
Each set size n is turned into [0/(n-1), 1/(n-1), ..., (n-2)/(n-1), 1, 0, 0, ..., 0, 0].
These are the ratios r at which f is evaluated at.
The 0s at the end are there for padding to the largest n in the batch.
If the input set x is passed in, it guarantees that the mask is the correct size even when sizes.max()
is less than x.size(), which can be a case if there is at least one padding element in each set in the batch.
"""
if x is not None:
max_size = x.size(2)
else:
max_size = sizes.max()
size_tensor = sizes.new(sizes.size(0), max_size).float().fill_(-1)
size_tensor = torch.arange(end=max_size, device=sizes.device, dtype=
torch.float32)
size_tensor = size_tensor.unsqueeze(0) / (sizes.float() - 1).clamp(min=1
).unsqueeze(1)
mask = size_tensor <= 1
mask = mask.unsqueeze(1)
return size_tensor.clamp(max=1), mask.float()
class FSPool(nn.Module):
"""
Featurewise sort pooling. From:
FSPool: Learning Set Representations with Featurewise Sort Pooling.
Yan Zhang, Jonathon Hare, Adam Prügel-Bennett
https://arxiv.org/abs/1906.02795
https://github.com/Cyanogenoid/fspool
"""
def __init__(self, in_channels, n_pieces, relaxed=False):
"""
in_channels: Number of channels in input
n_pieces: Number of pieces in piecewise linear
relaxed: Use sorting networks relaxation instead of traditional sorting
"""
super().__init__()
self.n_pieces = n_pieces
self.weight = nn.Parameter(torch.zeros(in_channels, n_pieces + 1))
self.relaxed = relaxed
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.weight)
def forward(self, x, n=None):
""" FSPool
x: FloatTensor of shape (batch_size, in_channels, set size).
This should contain the features of the elements in the set.
Variable set sizes should be padded to the maximum set size in the batch with 0s.
n: LongTensor of shape (batch_size).
This tensor contains the sizes of each set in the batch.
If not specified, assumes that every set has the same size of x.size(2).
Note that n.max() should never be greater than x.size(2), i.e. the specified set size in the
n tensor must not be greater than the number of elements stored in the x tensor.
Returns: pooled input x, used permutation matrix perm
"""
assert x.size(1) == self.weight.size(0
), 'incorrect number of input channels in weight'
if n is None:
n = x.new(x.size(0)).fill_(x.size(2)).long()
sizes, mask = fill_sizes(n, x)
mask = mask.expand_as(x)
weight = self.determine_weight(sizes)
x = x + (1 - mask).float() * -99999
if self.relaxed:
x, perm = cont_sort(x, temp=self.relaxed)
else:
x, perm = x.sort(dim=2, descending=True)
x = (x * weight * mask.float()).sum(dim=2)
return x, perm
def forward_transpose(self, x, perm, n=None):
""" FSUnpool
x: FloatTensor of shape (batch_size, in_channels)
perm: Permutation matrix returned by forward function.
n: LongTensor fo shape (batch_size)
"""
if n is None:
n = x.new(x.size(0)).fill_(perm.size(2)).long()
sizes, mask = fill_sizes(n)
mask = mask.expand(mask.size(0), x.size(1), mask.size(2))
weight = self.determine_weight(sizes)
x = x.unsqueeze(2) * weight * mask.float()
if self.relaxed:
x, _ = cont_sort(x, perm)
else:
x = x.scatter(2, perm, x)
return x, mask
def determine_weight(self, sizes):
"""
Piecewise linear function. Evaluates f at the ratios in sizes.
This should be a faster implementation than doing the sum over max terms, since we know that most terms in it are 0.
"""
weight = self.weight.unsqueeze(0)
weight = weight.expand(sizes.size(0), weight.size(1), weight.size(2))
index = self.n_pieces * sizes
index = index.unsqueeze(1)
index = index.expand(index.size(0), weight.size(1), index.size(2))
idx = index.long()
frac = index.frac()
left = weight.gather(2, idx)
right = weight.gather(2, (idx + 1).clamp(max=self.n_pieces))
return (1 - frac) * left + frac * right
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channels': 4, 'n_pieces': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
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_poi_fused__to_copy_0(out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
tmp0 = x0
tmp1 = tmp0.to(tl.float32)
tmp2 = 0.3333333333333333
tmp3 = tmp1 * tmp2
tmp4 = 1.0
tmp5 = triton_helpers.minimum(tmp3, tmp4)
tmp6 = 4.0
tmp7 = tmp5 * tmp6
tmp8 = tmp7.to(tl.int32)
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_clamp_1(out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
tmp0 = x0
tmp1 = tmp0.to(tl.float32)
tmp2 = 0.3333333333333333
tmp3 = tmp1 * tmp2
tmp4 = 1.0
tmp5 = triton_helpers.minimum(tmp3, tmp4)
tmp6 = 4.0
tmp7 = tmp5 * tmp6
tmp8 = tmp7.to(tl.int32)
tmp9 = tl.full([1], 1, tl.int64)
tmp10 = tmp8 + tmp9
tmp11 = tl.full([1], 4, tl.int64)
tmp12 = triton_helpers.minimum(tmp10, tmp11)
tl.store(out_ptr0 + x2, tmp12, xmask)
@triton.jit
def triton_per_fused_add_mul_rsub_sort_2(in_ptr0, out_ptr0, out_ptr1,
xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 64
RBLOCK: tl.constexpr = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + (x0 + 4 * r2 + 16 * x1), xmask, other=0.0)
tmp1 = x0
tmp2 = tmp1.to(tl.float32)
tmp3 = 0.3333333333333333
tmp4 = tmp2 * tmp3
tmp5 = 1.0
tmp6 = tmp4 <= tmp5
tmp7 = tmp6.to(tl.float32)
tmp8 = tmp5 - tmp7
tmp9 = -99999.0
tmp10 = tmp8 * tmp9
tmp11 = tmp0 + tmp10
tmp12 = r2
tmp13 = tmp12.to(tl.int16)
tmp14 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
tmp15 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
tmp16, tmp17 = triton_helpers.sort_with_index(tmp14, tmp15, None, 1,
stable=False, descending=True)
tl.store(out_ptr0 + (x0 + 4 * r2 + 16 * x1), tmp16, xmask)
tl.store(out_ptr1 + (x0 + 4 * r2 + 16 * x1), tmp17, xmask)
@triton.jit
def triton_poi_fused_sort_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
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tmp0.to(tl.int64)
tl.store(out_ptr0 + x0, tmp1, xmask)
@triton.jit
def triton_poi_fused_add_frac_gather_mul_rsub_4(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
x1 = xindex // 4 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = x0
tmp2 = tmp1.to(tl.float32)
tmp3 = 0.3333333333333333
tmp4 = tmp2 * tmp3
tmp5 = 1.0
tmp6 = triton_helpers.minimum(tmp4, tmp5)
tmp7 = 4.0
tmp8 = tmp6 * tmp7
tmp9 = tl_math.abs(tmp8)
tmp10 = libdevice.floor(tmp9)
tmp11 = tl.full([1], 0, tl.int32)
tmp12 = tmp11 < tmp8
tmp13 = tmp12.to(tl.int8)
tmp14 = tmp8 < tmp11
tmp15 = tmp14.to(tl.int8)
tmp16 = tmp13 - tmp15
tmp17 = tmp16.to(tmp8.dtype)
tmp18 = tmp10 * tmp17
tmp19 = tmp8 - tmp18
tmp20 = tmp5 - tmp19
tmp21 = tmp8.to(tl.int32)
tmp22 = tl.load(in_ptr1 + (tmp21 + 5 * x1), xmask, eviction_policy=
'evict_last')
tmp23 = tmp20 * tmp22
tmp24 = tl.full([1], 1, tl.int64)
tmp25 = tmp21 + tmp24
tmp26 = tl.full([1], 4, tl.int64)
tmp27 = triton_helpers.minimum(tmp25, tmp26)
tmp28 = tl.load(in_ptr1 + (tmp27 + 5 * x1), xmask, eviction_policy=
'evict_last')
tmp29 = tmp19 * tmp28
tmp30 = tmp23 + tmp29
tmp31 = tmp0 * tmp30
tmp32 = tmp4 <= tmp5
tmp33 = tmp32.to(tl.float32)
tmp34 = tmp31 * tmp33
tl.store(out_ptr0 + x3, tmp34, xmask)
@triton.jit
def triton_poi_fused_sum_5(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
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
primals_1, primals_2 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 5), (5, 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__to_copy_0[grid(64)](buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64)
triton_poi_fused_add_clamp_1[grid(64)](buf1, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int16)
triton_per_fused_add_mul_rsub_sort_2[grid(64)](primals_1, buf2,
buf3, 64, 4, XBLOCK=8, num_warps=2, num_stages=1)
del primals_1
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int64)
triton_poi_fused_sort_3[grid(256)](buf3, buf4, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf3
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_frac_gather_mul_rsub_4[grid(256)](buf2,
primals_2, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_sum_5[grid(64)](buf5, buf6, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf5
return buf6, buf4, buf0, buf1, buf2
def deterministic_sort(s, tau):
"""
"Stochastic Optimization of Sorting Networks via Continuous Relaxations" https://openreview.net/forum?id=H1eSS3CcKX
Aditya Grover, Eric Wang, Aaron Zweig, Stefano Ermon
s: input elements to be sorted. Shape: batch_size x n x 1
tau: temperature for relaxation. Scalar.
"""
n = s.size()[1]
one = torch.ones((n, 1), dtype=torch.float32, device=s.device)
A_s = torch.abs(s - s.permute(0, 2, 1))
B = torch.matmul(A_s, torch.matmul(one, one.transpose(0, 1)))
scaling = (n + 1 - 2 * (torch.arange(n, device=s.device) + 1)).type(torch
.float32)
C = torch.matmul(s, scaling.unsqueeze(0))
P_max = (C - B).permute(0, 2, 1)
sm = torch.nn.Softmax(-1)
P_hat = sm(P_max / tau)
return P_hat
def cont_sort(x, perm=None, temp=1):
""" Helper function that calls deterministic_sort with the right shape.
Since it assumes a shape of (batch_size, n, 1) while the input x is of shape (batch_size, channels, n),
we can get this to the right shape by merging the first two dimensions.
If an existing perm is passed in, we compute the "inverse" (transpose of perm) and just use that to unsort x.
"""
original_size = x.size()
x = x.view(-1, x.size(2), 1)
if perm is None:
perm = deterministic_sort(x, temp)
else:
perm = perm.transpose(1, 2)
x = perm.matmul(x)
x = x.view(original_size)
return x, perm
def fill_sizes(sizes, x=None):
"""
sizes is a LongTensor of size [batch_size], containing the set sizes.
Each set size n is turned into [0/(n-1), 1/(n-1), ..., (n-2)/(n-1), 1, 0, 0, ..., 0, 0].
These are the ratios r at which f is evaluated at.
The 0s at the end are there for padding to the largest n in the batch.
If the input set x is passed in, it guarantees that the mask is the correct size even when sizes.max()
is less than x.size(), which can be a case if there is at least one padding element in each set in the batch.
"""
if x is not None:
max_size = x.size(2)
else:
max_size = sizes.max()
size_tensor = sizes.new(sizes.size(0), max_size).float().fill_(-1)
size_tensor = torch.arange(end=max_size, device=sizes.device, dtype=
torch.float32)
size_tensor = size_tensor.unsqueeze(0) / (sizes.float() - 1).clamp(min=1
).unsqueeze(1)
mask = size_tensor <= 1
mask = mask.unsqueeze(1)
return size_tensor.clamp(max=1), mask.float()
class FSPoolNew(nn.Module):
"""
Featurewise sort pooling. From:
FSPool: Learning Set Representations with Featurewise Sort Pooling.
Yan Zhang, Jonathon Hare, Adam Prügel-Bennett
https://arxiv.org/abs/1906.02795
https://github.com/Cyanogenoid/fspool
"""
def __init__(self, in_channels, n_pieces, relaxed=False):
"""
in_channels: Number of channels in input
n_pieces: Number of pieces in piecewise linear
relaxed: Use sorting networks relaxation instead of traditional sorting
"""
super().__init__()
self.n_pieces = n_pieces
self.weight = nn.Parameter(torch.zeros(in_channels, n_pieces + 1))
self.relaxed = relaxed
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.weight)
def forward_transpose(self, x, perm, n=None):
""" FSUnpool
x: FloatTensor of shape (batch_size, in_channels)
perm: Permutation matrix returned by forward function.
n: LongTensor fo shape (batch_size)
"""
if n is None:
n = x.new(x.size(0)).fill_(perm.size(2)).long()
sizes, mask = fill_sizes(n)
mask = mask.expand(mask.size(0), x.size(1), mask.size(2))
weight = self.determine_weight(sizes)
x = x.unsqueeze(2) * weight * mask.float()
if self.relaxed:
x, _ = cont_sort(x, perm)
else:
x = x.scatter(2, perm, x)
return x, mask
def determine_weight(self, sizes):
"""
Piecewise linear function. Evaluates f at the ratios in sizes.
This should be a faster implementation than doing the sum over max terms, since we know that most terms in it are 0.
"""
weight = self.weight.unsqueeze(0)
weight = weight.expand(sizes.size(0), weight.size(1), weight.size(2))
index = self.n_pieces * sizes
index = index.unsqueeze(1)
index = index.expand(index.size(0), weight.size(1), index.size(2))
idx = index.long()
frac = index.frac()
left = weight.gather(2, idx)
right = weight.gather(2, (idx + 1).clamp(max=self.n_pieces))
return (1 - frac) * left + frac * right
def forward(self, input_0):
primals_2 = self.weight
primals_1 = input_0
output = call([primals_1, primals_2])
return output[0], output[1]
|
Cyanogenoid/dspn
|
FSPool
| false
| 13,576
|
[
"MIT"
] | 102
|
be3703b470ead46d76b70b4fed656c2e5343aff6
|
https://github.com/Cyanogenoid/dspn/tree/be3703b470ead46d76b70b4fed656c2e5343aff6
|
ResidualBlock
|
import torch
import torch.nn as nn
from functools import partial
def normalization(channels):
"""
Make a standard normalization layer.
:param channels: number of input channels.
:return: an nn.Module for normalization.
"""
return GroupNorm32(32, channels)
def ncsn_conv3x3(in_planes, out_planes, stride=1, bias=True, dilation=1,
init_scale=1.0, padding=1):
"""3x3 convolution with PyTorch initialization. Same as NCSNv1/NCSNv2."""
init_scale = 1e-10 if init_scale == 0 else init_scale
conv = nn.Conv2d(in_planes, out_planes, stride=stride, bias=bias,
dilation=dilation, padding=padding, kernel_size=3)
conv.weight.data *= init_scale
conv.bias.data *= init_scale
return conv
def ncsn_conv1x1(in_planes, out_planes, stride=1, bias=True, dilation=1,
init_scale=1.0, padding=0):
"""1x1 convolution. Same as NCSNv1/v2."""
conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
bias=bias, dilation=dilation, padding=padding)
init_scale = 1e-10 if init_scale == 0 else init_scale
conv.weight.data *= init_scale
conv.bias.data *= init_scale
return conv
class GroupNorm32(nn.GroupNorm):
def forward(self, x):
return super().forward(x.float()).type(x.dtype)
class ConvMeanPool(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True,
adjust_padding=False):
super().__init__()
if not adjust_padding:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = conv
else:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = nn.Sequential(nn.ZeroPad2d((1, 0, 1, 0)), conv)
def forward(self, inputs):
output = self.conv(inputs)
output = sum([output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]]) / 4.0
return output
class ResidualBlock(nn.Module):
def __init__(self, input_dim, output_dim, resample=None, act=nn.ELU(),
normalization=nn.InstanceNorm2d, adjust_padding=False, dilation=1):
super().__init__()
self.non_linearity = act
self.input_dim = input_dim
self.output_dim = output_dim
self.resample = resample
self.normalization = normalization
if resample == 'down':
if dilation > 1:
self.conv1 = ncsn_conv3x3(input_dim, input_dim, dilation=
dilation)
self.normalize2 = normalization(input_dim)
self.conv2 = ncsn_conv3x3(input_dim, output_dim, dilation=
dilation)
conv_shortcut = partial(ncsn_conv3x3, dilation=dilation)
else:
self.conv1 = ncsn_conv3x3(input_dim, input_dim)
self.normalize2 = normalization(input_dim)
self.conv2 = ConvMeanPool(input_dim, output_dim, 3,
adjust_padding=adjust_padding)
conv_shortcut = partial(ConvMeanPool, kernel_size=1,
adjust_padding=adjust_padding)
elif resample is None:
if dilation > 1:
conv_shortcut = partial(ncsn_conv3x3, dilation=dilation)
self.conv1 = ncsn_conv3x3(input_dim, output_dim, dilation=
dilation)
self.normalize2 = normalization(output_dim)
self.conv2 = ncsn_conv3x3(output_dim, output_dim, dilation=
dilation)
else:
conv_shortcut = partial(ncsn_conv1x1)
self.conv1 = ncsn_conv3x3(input_dim, output_dim)
self.normalize2 = normalization(output_dim)
self.conv2 = ncsn_conv3x3(output_dim, output_dim)
else:
raise Exception('invalid resample value')
if output_dim != input_dim or resample is not None:
self.shortcut = conv_shortcut(input_dim, output_dim)
self.normalize1 = normalization(input_dim)
def forward(self, x):
output = self.normalize1(x)
output = self.non_linearity(output)
output = self.conv1(output)
output = self.normalize2(output)
output = self.non_linearity(output)
output = self.conv2(output)
if self.output_dim == self.input_dim and self.resample is None:
shortcut = x
else:
shortcut = self.shortcut(x)
return shortcut + output
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
from functools import partial
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused__native_batch_norm_legit_elu_0(in_ptr0, out_ptr2,
xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tl.where(xmask, tmp1, 0)
tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
tmp6 = tl.where(xmask, tmp4, 0)
tmp7 = tl.sum(tmp6, 1)[:, None]
tmp8 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp9 = tmp8.to(tl.float32)
tmp10 = tmp7 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = tmp11 * tmp11
tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
tmp15 = tl.where(xmask, tmp13, 0)
tmp16 = tl.sum(tmp15, 1)[:, None]
tmp17 = tmp0 - tmp10
tmp18 = 16.0
tmp19 = tmp16 / tmp18
tmp20 = 1e-05
tmp21 = tmp19 + tmp20
tmp22 = libdevice.rsqrt(tmp21)
tmp23 = tmp17 * tmp22
tmp24 = 0.0
tmp25 = tmp23 > tmp24
tmp26 = 1.0
tmp27 = tmp23 * tmp26
tmp28 = libdevice.expm1(tmp27)
tmp29 = tmp28 * tmp26
tmp30 = tl.where(tmp25, tmp27, tmp29)
tl.store(out_ptr2 + (r1 + 16 * x0), tmp30, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_elu_1(in_out_ptr0,
in_out_ptr1, in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.
constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
x3 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tl.where(xmask, tmp3, 0)
tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tmp8 = tl.where(xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp3 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.where(xmask, tmp15, 0)
tmp18 = tl.sum(tmp17, 1)[:, None]
tmp19 = 16.0
tmp20 = tmp18 / tmp19
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tmp24 = tmp2 - tmp12
tmp25 = tmp24 * tmp23
tmp26 = 0.0
tmp27 = tmp25 > tmp26
tmp28 = 1.0
tmp29 = tmp25 * tmp28
tmp30 = libdevice.expm1(tmp29)
tmp31 = tmp30 * tmp28
tmp32 = tl.where(tmp27, tmp29, tmp31)
tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp23, xmask)
tl.store(out_ptr1 + (r2 + 16 * x3), tmp32, xmask)
tl.store(out_ptr0 + x3, tmp12, xmask)
@triton.jit
def triton_poi_fused_add_convolution_2(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_out_ptr0 + x3, xmask)
tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tl.store(in_out_ptr0 + x3, tmp4, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_per_fused__native_batch_norm_legit_elu_0[grid(16)](primals_1,
buf3, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
buf4 = extern_kernels.convolution(buf3, primals_2, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1))
buf5 = buf4
del buf4
buf6 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32)
buf7 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
)
buf9 = reinterpret_tensor(buf7, (1, 16, 1, 1), (16, 1, 1, 1), 0)
del buf7
buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_per_fused__native_batch_norm_legit_convolution_elu_1[grid(16)](
buf5, buf9, primals_3, buf6, buf10, 16, 16, XBLOCK=8, num_warps
=2, num_stages=1)
del primals_3
buf11 = extern_kernels.convolution(buf10, primals_4, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf11, (4, 4, 4, 4), (64, 16, 4, 1))
buf12 = buf11
del buf11
triton_poi_fused_add_convolution_2[grid(256)](buf12, primals_1,
primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
del primals_5
return buf12, primals_2, primals_4, buf3, buf5, buf6, buf9, buf10
def normalization(channels):
"""
Make a standard normalization layer.
:param channels: number of input channels.
:return: an nn.Module for normalization.
"""
return GroupNorm32(32, channels)
def ncsn_conv3x3(in_planes, out_planes, stride=1, bias=True, dilation=1,
init_scale=1.0, padding=1):
"""3x3 convolution with PyTorch initialization. Same as NCSNv1/NCSNv2."""
init_scale = 1e-10 if init_scale == 0 else init_scale
conv = nn.Conv2d(in_planes, out_planes, stride=stride, bias=bias,
dilation=dilation, padding=padding, kernel_size=3)
conv.weight.data *= init_scale
conv.bias.data *= init_scale
return conv
def ncsn_conv1x1(in_planes, out_planes, stride=1, bias=True, dilation=1,
init_scale=1.0, padding=0):
"""1x1 convolution. Same as NCSNv1/v2."""
conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
bias=bias, dilation=dilation, padding=padding)
init_scale = 1e-10 if init_scale == 0 else init_scale
conv.weight.data *= init_scale
conv.bias.data *= init_scale
return conv
class GroupNorm32(nn.GroupNorm):
def forward(self, x):
return super().forward(x.float()).type(x.dtype)
class ConvMeanPool(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True,
adjust_padding=False):
super().__init__()
if not adjust_padding:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = conv
else:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = nn.Sequential(nn.ZeroPad2d((1, 0, 1, 0)), conv)
def forward(self, inputs):
output = self.conv(inputs)
output = sum([output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]]) / 4.0
return output
class ResidualBlockNew(nn.Module):
def __init__(self, input_dim, output_dim, resample=None, act=nn.ELU(),
normalization=nn.InstanceNorm2d, adjust_padding=False, dilation=1):
super().__init__()
self.non_linearity = act
self.input_dim = input_dim
self.output_dim = output_dim
self.resample = resample
self.normalization = normalization
if resample == 'down':
if dilation > 1:
self.conv1 = ncsn_conv3x3(input_dim, input_dim, dilation=
dilation)
self.normalize2 = normalization(input_dim)
self.conv2 = ncsn_conv3x3(input_dim, output_dim, dilation=
dilation)
conv_shortcut = partial(ncsn_conv3x3, dilation=dilation)
else:
self.conv1 = ncsn_conv3x3(input_dim, input_dim)
self.normalize2 = normalization(input_dim)
self.conv2 = ConvMeanPool(input_dim, output_dim, 3,
adjust_padding=adjust_padding)
conv_shortcut = partial(ConvMeanPool, kernel_size=1,
adjust_padding=adjust_padding)
elif resample is None:
if dilation > 1:
conv_shortcut = partial(ncsn_conv3x3, dilation=dilation)
self.conv1 = ncsn_conv3x3(input_dim, output_dim, dilation=
dilation)
self.normalize2 = normalization(output_dim)
self.conv2 = ncsn_conv3x3(output_dim, output_dim, dilation=
dilation)
else:
conv_shortcut = partial(ncsn_conv1x1)
self.conv1 = ncsn_conv3x3(input_dim, output_dim)
self.normalize2 = normalization(output_dim)
self.conv2 = ncsn_conv3x3(output_dim, output_dim)
else:
raise Exception('invalid resample value')
if output_dim != input_dim or resample is not None:
self.shortcut = conv_shortcut(input_dim, output_dim)
self.normalize1 = normalization(input_dim)
def forward(self, input_0):
primals_2 = self.conv1.weight
primals_3 = self.conv1.bias
primals_4 = self.conv2.weight
primals_5 = self.conv2.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DeepTitan/PNDM
|
ResidualBlock
| false
| 13,577
|
[
"Apache-2.0"
] | 61
|
4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
https://github.com/DeepTitan/PNDM/tree/4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
h_sigmoid
|
import torch
import torch.nn as nn
from itertools import product as product
import torch.nn.parallel
import torch.utils.data
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
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
from itertools import product as product
import torch.nn.parallel
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_div_hardtanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 3.0
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp5 = 6.0
tmp6 = triton_helpers.minimum(tmp4, tmp5)
tmp7 = 0.16666666666666666
tmp8 = tmp6 * tmp7
tl.store(out_ptr0 + x0, tmp8, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_hardtanh_0[grid(256)](arg0_1, buf0, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class h_sigmoidNew(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoidNew, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DefTruth/PIPNet
|
h_sigmoid
| false
| 13,578
|
[
"MIT"
] | 162
|
a1fb1e229319dac0069e37eb8fb4278d454edbb0
|
https://github.com/DefTruth/PIPNet/tree/a1fb1e229319dac0069e37eb8fb4278d454edbb0
|
UpsampleConv
|
import torch
import torch.nn as nn
class UpsampleConv(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True):
super().__init__()
self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.pixelshuffle = nn.PixelShuffle(upscale_factor=2)
def forward(self, inputs):
output = inputs
output = torch.cat([output, output, output, output], dim=1)
output = self.pixelshuffle(output)
return self.conv(output)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_pixel_shuffle_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 % 2
x1 = xindex // 2 % 4
x2 = xindex // 8 % 2
x3 = xindex // 16 % 4
x5 = xindex // 256
x6 = xindex
tmp0 = tl.load(in_ptr0 + (x1 + 4 * x3 + 16 * x0 + 32 * x2 + 64 * x5),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x6, tmp0, 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 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 2, 4, 2), (256, 64, 16, 8, 2, 1
), torch.float32)
get_raw_stream(0)
triton_poi_fused_pixel_shuffle_0[grid(1024)](primals_1, buf0, 1024,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (4, 4, 8,
8), (256, 64, 8, 1), 0), primals_2, stride=(1, 1), padding=(1,
1), dilation=(1, 1), transposed=False, output_padding=(0, 0),
groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 8, 8), (256, 64, 8, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(1024)](buf2, primals_3, 1024,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
return buf2, primals_2, reinterpret_tensor(buf0, (4, 4, 8, 8), (256, 64,
8, 1), 0)
class UpsampleConvNew(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True):
super().__init__()
self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.pixelshuffle = nn.PixelShuffle(upscale_factor=2)
def forward(self, input_0):
primals_2 = self.conv.weight
primals_3 = self.conv.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DeepTitan/PNDM
|
UpsampleConv
| false
| 13,579
|
[
"Apache-2.0"
] | 61
|
4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
https://github.com/DeepTitan/PNDM/tree/4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
Net
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(3 * 28 * 28, 512)
self.fc2 = nn.Linear(512, 512)
self.fc3 = nn.Linear(512, 1)
def forward(self, x):
x = x.view(-1, 3 * 28 * 28)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
logits = self.fc3(x).flatten()
return logits
def get_inputs():
return [torch.rand([4, 2352])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 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)
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, 2352), (2352, 1))
assert_size_stride(primals_2, (512, 2352), (2352, 1))
assert_size_stride(primals_3, (512,), (1,))
assert_size_stride(primals_4, (512, 512), (512, 1))
assert_size_stride(primals_5, (512,), (1,))
assert_size_stride(primals_6, (1, 512), (512, 1))
assert_size_stride(primals_7, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 512), (512, 1), torch.float32)
extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (2352,
512), (1, 2352), 0), out=buf0)
del primals_2
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_relu_0[grid(2048)](buf1, primals_3, 2048, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_3
buf2 = empty_strided_cuda((4, 512), (512, 1), torch.float32)
extern_kernels.mm(buf1, reinterpret_tensor(primals_4, (512, 512), (
1, 512), 0), out=buf2)
buf3 = buf2
del buf2
triton_poi_fused_relu_0[grid(2048)](buf3, primals_5, 2048, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_5
buf5 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_7, buf3, reinterpret_tensor(primals_6,
(512, 1), (1, 512), 0), alpha=1, beta=1, out=buf5)
del primals_7
return reinterpret_tensor(buf5, (4,), (1,), 0
), primals_1, buf1, buf3, primals_6, primals_4
class NetNew(nn.Module):
def __init__(self):
super(NetNew, self).__init__()
self.fc1 = nn.Linear(3 * 28 * 28, 512)
self.fc2 = nn.Linear(512, 512)
self.fc3 = nn.Linear(512, 1)
def forward(self, input_0):
primals_2 = self.fc1.weight
primals_3 = self.fc1.bias
primals_4 = self.fc2.weight
primals_5 = self.fc2.bias
primals_6 = self.fc3.weight
primals_7 = self.fc3.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
DavyMorgan/invariant-risk-minimization
|
Net
| false
| 13,580
|
[
"MIT"
] | 77
|
d0fe48e75329561e6b2d47dbc97042aa740f77c2
|
https://github.com/DavyMorgan/invariant-risk-minimization/tree/d0fe48e75329561e6b2d47dbc97042aa740f77c2
|
CAModule
|
import torch
from torch import nn
class CAModule(nn.Module):
"""
Re-implementation of Squeeze-and-Excitation (SE) block described in:
*Hu et al., Squeeze-and-Excitation Networks, arXiv:1709.01507*
code reference:
https://github.com/kobiso/CBAM-keras/blob/master/models/attention_module.py
"""
def __init__(self, num_channels, reduc_ratio=2):
super(CAModule, self).__init__()
self.num_channels = num_channels
self.reduc_ratio = reduc_ratio
self.fc1 = nn.Linear(num_channels, num_channels // reduc_ratio,
bias=True)
self.fc2 = nn.Linear(num_channels // reduc_ratio, num_channels,
bias=True)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, feat_map):
gap_out = feat_map.view(feat_map.size()[0], self.num_channels, -1
).mean(dim=2)
fc1_out = self.relu(self.fc1(gap_out))
fc2_out = self.sigmoid(self.fc2(fc1_out))
fc2_out = fc2_out.view(fc2_out.size()[0], fc2_out.size()[1], 1, 1)
feat_map = torch.mul(feat_map, fc2_out)
return feat_map
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'num_channels': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK:
tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tmp5 = 16.0
tmp6 = tmp4 / tmp5
tl.debug_barrier()
tl.store(in_out_ptr0 + x0, tmp6, xmask)
@triton.jit
def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 8
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 2
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_mul_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 16
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp2 = tl.sigmoid(tmp1)
tmp3 = tmp0 * tmp2
tl.store(out_ptr0 + x2, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (2, 4), (4, 1))
assert_size_stride(primals_3, (2,), (1,))
assert_size_stride(primals_4, (4, 2), (2, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_mean_0[grid(16)](buf1, primals_1, 16, 16, XBLOCK=1,
num_warps=2, num_stages=1)
buf2 = empty_strided_cuda((4, 2), (2, 1), torch.float32)
extern_kernels.mm(buf1, reinterpret_tensor(primals_2, (4, 2), (1, 4
), 0), out=buf2)
del primals_2
buf3 = buf2
del buf2
triton_poi_fused_relu_1[grid(8)](buf3, primals_3, 8, XBLOCK=8,
num_warps=1, num_stages=1)
del primals_3
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, buf3, reinterpret_tensor(primals_4,
(2, 4), (1, 2), 0), alpha=1, beta=1, out=buf4)
del primals_5
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_mul_2[grid(256)](primals_1, buf4, buf5, 256,
XBLOCK=128, num_warps=4, num_stages=1)
return buf5, primals_1, buf1, buf3, buf4, primals_4
class CAModuleNew(nn.Module):
"""
Re-implementation of Squeeze-and-Excitation (SE) block described in:
*Hu et al., Squeeze-and-Excitation Networks, arXiv:1709.01507*
code reference:
https://github.com/kobiso/CBAM-keras/blob/master/models/attention_module.py
"""
def __init__(self, num_channels, reduc_ratio=2):
super(CAModuleNew, self).__init__()
self.num_channels = num_channels
self.reduc_ratio = reduc_ratio
self.fc1 = nn.Linear(num_channels, num_channels // reduc_ratio,
bias=True)
self.fc2 = nn.Linear(num_channels // reduc_ratio, num_channels,
bias=True)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, input_0):
primals_2 = self.fc1.weight
primals_3 = self.fc1.bias
primals_4 = self.fc2.weight
primals_5 = self.fc2.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DavidChenL/Chexpert
|
CAModule
| false
| 13,581
|
[
"Apache-2.0"
] | 202
|
0300057d3a51301cff35a65f79729436678b4a79
|
https://github.com/DavidChenL/Chexpert/tree/0300057d3a51301cff35a65f79729436678b4a79
|
h_swish
|
import torch
import torch.nn as nn
from itertools import product as product
import torch.nn.parallel
import torch.utils.data
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swish(nn.Module):
def __init__(self, inplace=True):
super(h_swish, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, x):
return x * self.sigmoid(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
from itertools import product as product
import torch.nn.parallel
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_div_hardtanh_mul_0(in_ptr0, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 3.0
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp5 = 6.0
tmp6 = triton_helpers.minimum(tmp4, tmp5)
tmp7 = 0.16666666666666666
tmp8 = tmp6 * tmp7
tmp9 = tmp0 * tmp8
tl.store(out_ptr0 + x0, tmp9, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_hardtanh_mul_0[grid(256)](arg0_1, buf0,
256, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swishNew(nn.Module):
def __init__(self, inplace=True):
super(h_swishNew, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DefTruth/PIPNet
|
h_swish
| false
| 13,582
|
[
"MIT"
] | 162
|
a1fb1e229319dac0069e37eb8fb4278d454edbb0
|
https://github.com/DefTruth/PIPNet/tree/a1fb1e229319dac0069e37eb8fb4278d454edbb0
|
ConvMeanPool
|
import torch
import torch.nn as nn
class ConvMeanPool(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True,
adjust_padding=False):
super().__init__()
if not adjust_padding:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = conv
else:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = nn.Sequential(nn.ZeroPad2d((1, 0, 1, 0)), conv)
def forward(self, inputs):
output = self.conv(inputs)
output = sum([output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]]) / 4.0
return output
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_dim': 4, 'output_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 2
x4 = xindex // 2
x2 = xindex // 4 % 4
x5 = xindex
tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x4), xmask, eviction_policy=
'evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x4), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x4), xmask, eviction_policy=
'evict_last')
tmp11 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x4), xmask, eviction_policy
='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 + tmp3
tmp6 = tmp5 + tmp1
tmp7 = tmp4 + tmp6
tmp9 = tmp8 + tmp1
tmp10 = tmp7 + tmp9
tmp12 = tmp11 + tmp1
tmp13 = tmp10 + tmp12
tmp14 = 0.25
tmp15 = tmp13 * tmp14
tl.store(out_ptr0 + x5, tmp15, 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, 2, 2), (16, 4, 2, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_0[grid(64)](buf0, primals_2, buf1, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del buf0
del primals_2
return buf1, primals_1, primals_3
class ConvMeanPoolNew(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True,
adjust_padding=False):
super().__init__()
if not adjust_padding:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = conv
else:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1,
padding=kernel_size // 2, bias=biases)
self.conv = nn.Sequential(nn.ZeroPad2d((1, 0, 1, 0)), conv)
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]
|
DeepTitan/PNDM
|
ConvMeanPool
| false
| 13,583
|
[
"Apache-2.0"
] | 61
|
4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
https://github.com/DeepTitan/PNDM/tree/4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
InstanceNorm2dPlus
|
import torch
import torch.nn as nn
class InstanceNorm2dPlus(nn.Module):
def __init__(self, num_features, bias=True):
super().__init__()
self.num_features = num_features
self.bias = bias
self.instance_norm = nn.InstanceNorm2d(num_features, affine=False,
track_running_stats=False)
self.alpha = nn.Parameter(torch.zeros(num_features))
self.gamma = nn.Parameter(torch.zeros(num_features))
self.alpha.data.normal_(1, 0.02)
self.gamma.data.normal_(1, 0.02)
if bias:
self.beta = nn.Parameter(torch.zeros(num_features))
def forward(self, x):
means = torch.mean(x, dim=(2, 3))
m = torch.mean(means, dim=-1, keepdim=True)
v = torch.var(means, dim=-1, keepdim=True)
means = (means - m) / torch.sqrt(v + 1e-05)
h = self.instance_norm(x)
if self.bias:
h = h + means[..., None, None] * self.alpha[..., None, None]
out = self.gamma.view(-1, self.num_features, 1, 1
) * h + self.beta.view(-1, self.num_features, 1, 1)
else:
h = h + means[..., None, None] * self.alpha[..., None, None]
out = self.gamma.view(-1, self.num_features, 1, 1) * h
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'num_features': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.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__native_batch_norm_legit_mean_0(in_out_ptr0, in_ptr0,
out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tmp6 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
tmp8 = tl.where(xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp1 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.where(xmask, tmp15, 0)
tmp18 = tl.sum(tmp17, 1)[:, None]
tmp19 = 16.0
tmp20 = tmp18 / tmp19
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tl.debug_barrier()
tl.store(in_out_ptr0 + x0, tmp23, xmask)
tl.store(out_ptr0 + x0, tmp4, xmask)
tl.store(out_ptr1 + x0, tmp12, xmask)
@triton.jit
def triton_poi_fused_add_div_mean_sqrt_sub_var_1(in_ptr0, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
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 = 16.0
tmp2 = tmp0 / tmp1
tmp4 = tmp3 / tmp1
tmp6 = tmp5 / tmp1
tmp7 = tmp4 + tmp6
tmp9 = tmp8 / tmp1
tmp10 = tmp7 + tmp9
tmp12 = tmp11 / tmp1
tmp13 = tmp10 + tmp12
tmp14 = 4.0
tmp15 = tmp13 / tmp14
tmp16 = tmp2 - tmp15
tmp17 = tmp4 - tmp15
tmp18 = tmp17 * tmp17
tmp19 = tmp6 - tmp15
tmp20 = tmp19 * tmp19
tmp21 = tmp18 + tmp20
tmp22 = tmp9 - tmp15
tmp23 = tmp22 * tmp22
tmp24 = tmp21 + tmp23
tmp25 = tmp12 - tmp15
tmp26 = tmp25 * tmp25
tmp27 = tmp24 + tmp26
tmp28 = 3.0
tmp29 = tmp27 / tmp28
tmp30 = 1e-05
tmp31 = tmp29 + tmp30
tmp32 = libdevice.sqrt(tmp31)
tmp33 = tmp16 / tmp32
tl.store(out_ptr0 + x2, tmp33, xmask)
@triton.jit
def triton_poi_fused_add_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
in_ptr5, in_ptr6, 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 // 16 % 4
x3 = xindex
x4 = xindex // 16
tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
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 + x4, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr5 + x1, xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr6 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 - tmp2
tmp5 = tmp3 * tmp4
tmp8 = tmp6 * tmp7
tmp9 = tmp5 + tmp8
tmp10 = tmp0 * tmp9
tmp12 = tmp10 + tmp11
tl.store(out_ptr0 + x3, tmp12, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf2 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32)
buf3 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
)
buf5 = reinterpret_tensor(buf3, (1, 16, 1, 1), (16, 1, 1, 1), 0)
del buf3
get_raw_stream(0)
triton_per_fused__native_batch_norm_legit_mean_0[grid(16)](buf5,
primals_1, buf0, buf2, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_div_mean_sqrt_sub_var_1[grid(16)](buf0, buf1,
16, XBLOCK=16, num_warps=1, num_stages=1)
del buf0
buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused_add_mul_2[grid(256)](primals_3, primals_1, buf2,
buf5, buf1, primals_2, primals_4, buf6, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del primals_4
return (buf6, primals_1, primals_2, primals_3, buf2, buf5,
reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0))
class InstanceNorm2dPlusNew(nn.Module):
def __init__(self, num_features, bias=True):
super().__init__()
self.num_features = num_features
self.bias = bias
self.instance_norm = nn.InstanceNorm2d(num_features, affine=False,
track_running_stats=False)
self.alpha = nn.Parameter(torch.zeros(num_features))
self.gamma = nn.Parameter(torch.zeros(num_features))
self.alpha.data.normal_(1, 0.02)
self.gamma.data.normal_(1, 0.02)
if bias:
self.beta = nn.Parameter(torch.zeros(num_features))
def forward(self, input_0):
primals_2 = self.alpha
primals_3 = self.gamma
primals_4 = self.beta
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
DeepTitan/PNDM
|
InstanceNorm2dPlus
| false
| 13,584
|
[
"Apache-2.0"
] | 61
|
4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
https://github.com/DeepTitan/PNDM/tree/4037a4f40011c9a0d47b92303e64d47fcc7ed56a
|
LayerNorm
|
import torch
import torch.nn as nn
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
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_std_sub_0(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 4.0
tmp10 = tmp8 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = tmp0 * tmp11
tmp13 = tmp2 - tmp10
tmp14 = tmp13 * tmp13
tmp15 = tmp3 - tmp10
tmp16 = tmp15 * tmp15
tmp17 = tmp14 + tmp16
tmp18 = tmp5 - tmp10
tmp19 = tmp18 * tmp18
tmp20 = tmp17 + tmp19
tmp21 = tmp7 - tmp10
tmp22 = tmp21 * tmp21
tmp23 = tmp20 + tmp22
tmp24 = 3.0
tmp25 = tmp23 / tmp24
tmp26 = libdevice.sqrt(tmp25)
tmp27 = 1e-06
tmp28 = tmp26 + tmp27
tmp29 = tmp12 / tmp28
tmp31 = tmp29 + tmp30
tl.store(out_ptr0 + x2, tmp31, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_mean_mul_std_sub_0[grid(256)](primals_2,
primals_1, primals_3, buf0, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del primals_2
del primals_3
return buf0, primals_1
class LayerNormNew(nn.Module):
def __init__(self, features, eps=1e-06):
super(LayerNormNew, self).__init__()
self.gamma = nn.Parameter(torch.ones(features))
self.beta = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, input_0):
primals_2 = self.gamma
primals_3 = self.beta
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
Dodger23/SincNet
|
LayerNorm
| false
| 13,585
|
[
"MIT"
] | 951
|
bf848e88dc8d6cbeb4484e89486ec0a4ab237cb1
|
https://github.com/Dodger23/SincNet/tree/bf848e88dc8d6cbeb4484e89486ec0a4ab237cb1
|
ParamSum
|
import torch
import torch.utils.data
import torch
from torch import nn
def resize(x1, x2, largest=True):
if largest:
if x1.size()[2:] > x2.size()[2:]:
x2 = nn.Upsample(size=x1.size()[2:], mode='bilinear')(x2)
elif x1.size()[2:] < x2.size()[2:]:
x1 = nn.Upsample(size=x2.size()[2:], mode='bilinear')(x1)
return x1, x2
else:
raise NotImplementedError
class ParamSum(nn.Module):
def __init__(self, C):
super(ParamSum, self).__init__()
self.a = nn.Parameter(torch.ones(C))
self.b = nn.Parameter(torch.ones(C))
def forward(self, x, y):
bsize = x.size(0)
x, y = resize(x, y)
return self.a.expand(bsize, -1)[:, :, None, None] * x + self.b.expand(
bsize, -1)[:, :, None, None] * y
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'C': 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.utils.data
import torch
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_mul_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 16 % 4
x3 = xindex
tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x3, xmask)
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr3 + x3, xmask)
tmp2 = tmp0 * tmp1
tmp5 = tmp3 * tmp4
tmp6 = tmp2 + tmp5
tl.store(out_ptr0 + x3, tmp6, 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, (4,), (1,))
assert_size_stride(primals_4, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_mul_0[grid(256)](primals_3, primals_1,
primals_4, primals_2, buf0, 256, XBLOCK=256, num_warps=4,
num_stages=1)
del primals_3
del primals_4
return buf0, primals_1, primals_2
def resize(x1, x2, largest=True):
if largest:
if x1.size()[2:] > x2.size()[2:]:
x2 = nn.Upsample(size=x1.size()[2:], mode='bilinear')(x2)
elif x1.size()[2:] < x2.size()[2:]:
x1 = nn.Upsample(size=x2.size()[2:], mode='bilinear')(x1)
return x1, x2
else:
raise NotImplementedError
class ParamSumNew(nn.Module):
def __init__(self, C):
super(ParamSumNew, self).__init__()
self.a = nn.Parameter(torch.ones(C))
self.b = nn.Parameter(torch.ones(C))
def forward(self, input_0, input_1):
primals_3 = self.a
primals_4 = self.b
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
DominickZhang/NAS-FCOS
|
ParamSum
| false
| 13,586
|
[
"BSD-2-Clause"
] | 187
|
1f7281478430eaed028e2cc2dfa8be226c63939b
|
https://github.com/DominickZhang/NAS-FCOS/tree/1f7281478430eaed028e2cc2dfa8be226c63939b
|
Loss
|
import torch
import torch.nn as nn
class Loss(nn.Module):
def __init__(self, lambd):
super(Loss, self).__init__()
self.lambd = lambd
self.lsm = nn.LogSoftmax(dim=1)
def forward(self, O, Y, C):
return (Y * (self.lambd * C - self.lsm(O))).mean(dim=0).sum()
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 [[], {'lambd': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
@triton.jit
def triton_per_fused__log_softmax_mean_mul_sub_sum_1(in_ptr0, in_ptr1,
in_ptr2, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
r0 = rindex % 16
tmp0 = tl.load(in_ptr0 + r2, None)
tmp1 = tl.load(in_ptr1 + r2, None)
tmp4 = tl.load(in_ptr2 + r2, None)
tmp5 = tl.load(in_ptr2 + r0, None, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr2 + (16 + r0), None, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr2 + (32 + r0), None, eviction_policy='evict_last')
tmp13 = tl.load(in_ptr2 + (48 + r0), None, eviction_policy='evict_last')
tmp20 = tl.load(in_ptr0 + (64 + r2), None)
tmp21 = tl.load(in_ptr1 + (64 + r2), None)
tmp23 = tl.load(in_ptr2 + (64 + r2), None)
tmp24 = tl.load(in_ptr2 + (64 + r0), None, eviction_policy='evict_last')
tmp26 = tl.load(in_ptr2 + (80 + r0), None, eviction_policy='evict_last')
tmp29 = tl.load(in_ptr2 + (96 + r0), None, eviction_policy='evict_last')
tmp32 = tl.load(in_ptr2 + (112 + r0), None, eviction_policy='evict_last')
tmp40 = tl.load(in_ptr0 + (128 + r2), None)
tmp41 = tl.load(in_ptr1 + (128 + r2), None)
tmp43 = tl.load(in_ptr2 + (128 + r2), None)
tmp44 = tl.load(in_ptr2 + (128 + r0), None, eviction_policy='evict_last')
tmp46 = tl.load(in_ptr2 + (144 + r0), None, eviction_policy='evict_last')
tmp49 = tl.load(in_ptr2 + (160 + r0), None, eviction_policy='evict_last')
tmp52 = tl.load(in_ptr2 + (176 + r0), None, eviction_policy='evict_last')
tmp60 = tl.load(in_ptr0 + (192 + r2), None)
tmp61 = tl.load(in_ptr1 + (192 + r2), None)
tmp63 = tl.load(in_ptr2 + (192 + r2), None)
tmp64 = tl.load(in_ptr2 + (192 + r0), None, eviction_policy='evict_last')
tmp66 = tl.load(in_ptr2 + (208 + r0), None, eviction_policy='evict_last')
tmp69 = tl.load(in_ptr2 + (224 + r0), None, eviction_policy='evict_last')
tmp72 = tl.load(in_ptr2 + (240 + r0), None, eviction_policy='evict_last')
tmp2 = 4.0
tmp3 = tmp1 * tmp2
tmp6 = tl_math.exp(tmp5)
tmp8 = tl_math.exp(tmp7)
tmp9 = tmp6 + tmp8
tmp11 = tl_math.exp(tmp10)
tmp12 = tmp9 + tmp11
tmp14 = tl_math.exp(tmp13)
tmp15 = tmp12 + tmp14
tmp16 = tl_math.log(tmp15)
tmp17 = tmp4 - tmp16
tmp18 = tmp3 - tmp17
tmp19 = tmp0 * tmp18
tmp22 = tmp21 * tmp2
tmp25 = tl_math.exp(tmp24)
tmp27 = tl_math.exp(tmp26)
tmp28 = tmp25 + tmp27
tmp30 = tl_math.exp(tmp29)
tmp31 = tmp28 + tmp30
tmp33 = tl_math.exp(tmp32)
tmp34 = tmp31 + tmp33
tmp35 = tl_math.log(tmp34)
tmp36 = tmp23 - tmp35
tmp37 = tmp22 - tmp36
tmp38 = tmp20 * tmp37
tmp39 = tmp19 + tmp38
tmp42 = tmp41 * tmp2
tmp45 = tl_math.exp(tmp44)
tmp47 = tl_math.exp(tmp46)
tmp48 = tmp45 + tmp47
tmp50 = tl_math.exp(tmp49)
tmp51 = tmp48 + tmp50
tmp53 = tl_math.exp(tmp52)
tmp54 = tmp51 + tmp53
tmp55 = tl_math.log(tmp54)
tmp56 = tmp43 - tmp55
tmp57 = tmp42 - tmp56
tmp58 = tmp40 * tmp57
tmp59 = tmp39 + tmp58
tmp62 = tmp61 * tmp2
tmp65 = tl_math.exp(tmp64)
tmp67 = tl_math.exp(tmp66)
tmp68 = tmp65 + tmp67
tmp70 = tl_math.exp(tmp69)
tmp71 = tmp68 + tmp70
tmp73 = tl_math.exp(tmp72)
tmp74 = tmp71 + tmp73
tmp75 = tl_math.log(tmp74)
tmp76 = tmp63 - tmp75
tmp77 = tmp62 - tmp76
tmp78 = tmp60 * tmp77
tmp79 = tmp59 + tmp78
tmp80 = tmp79 / tmp2
tmp81 = tl.broadcast_to(tmp80, [XBLOCK, RBLOCK])
tmp83 = tl.sum(tmp81, 1)[:, None]
tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp83, None)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__log_softmax_0[grid(256)](arg1_1, buf0, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del arg1_1
buf2 = empty_strided_cuda((), (), torch.float32)
triton_per_fused__log_softmax_mean_mul_sub_sum_1[grid(1)](arg2_1,
arg0_1, buf0, buf2, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
del arg2_1
del buf0
return buf2,
class LossNew(nn.Module):
def __init__(self, lambd):
super(LossNew, self).__init__()
self.lambd = lambd
self.lsm = nn.LogSoftmax(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]
|
DmZhukov/CrossTask
|
Loss
| false
| 13,587
|
[
"BSD-3-Clause"
] | 58
|
2d79941d687dc8bd100898acd9c71c476b99def1
|
https://github.com/DmZhukov/CrossTask/tree/2d79941d687dc8bd100898acd9c71c476b99def1
|
PositionwiseFeedForward
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class PointwiseConv(nn.Module):
"""
Pointwise Convolution (1x1 Conv)
Convolution 1 Dimension (Faster version)
(cf. https://github.com/huggingface/pytorch-openai-transformer-lm/blob/ eafc28abdfadfa0732f03a0fc65805c5bfb2ffe7/model_pytorch.py#L45)
* Args:
input_size: the number of input tensor's dimension
num_filters: the number of convolution filter
"""
def __init__(self, input_size, num_filters):
super(PointwiseConv, self).__init__()
self.kernel_size = 1
self.num_filters = num_filters
weight = torch.empty(input_size, num_filters)
nn.init.normal_(weight, std=0.02)
self.weight = nn.Parameter(weight)
self.bias = nn.Parameter(torch.zeros(num_filters))
def forward(self, x):
size_out = x.size()[:-1] + (self.num_filters,)
x = torch.addmm(self.bias, x.contiguous().view(-1, x.size(-1)),
self.weight)
x = x.view(*size_out)
return x
class PositionwiseFeedForward(nn.Module):
"""
Pointwise Feed-Forward Layer
* Args:
input_size: the number of input size
hidden_size: the number of hidden size
* Kwargs:
dropout: the probability of dropout
"""
def __init__(self, input_size, hidden_size, dropout=0.1):
super(PositionwiseFeedForward, self).__init__()
self.pointwise_conv1 = PointwiseConv(input_size=input_size,
num_filters=hidden_size)
self.pointwise_conv2 = PointwiseConv(input_size=hidden_size,
num_filters=input_size)
self.activation_fn = F.relu
self.dropout = nn.Dropout(p=dropout)
def forward(self, x):
x = self.pointwise_conv1(x)
x = self.activation_fn(x)
x = self.pointwise_conv2(x)
x = self.dropout(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'hidden_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
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 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4,), (1,))
assert_size_stride(primals_5, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
primals_3, out=buf0)
del primals_3
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1,
primals_2, buf3, 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_4, reinterpret_tensor(buf1, (64, 4), (
4, 1), 0), primals_5, alpha=1, beta=1, out=buf2)
del primals_4
return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0
), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0
), reinterpret_tensor(buf1, (4, 64), (1, 4), 0
), buf3, reinterpret_tensor(primals_1, (4, 64), (1, 4), 0)
class PointwiseConv(nn.Module):
"""
Pointwise Convolution (1x1 Conv)
Convolution 1 Dimension (Faster version)
(cf. https://github.com/huggingface/pytorch-openai-transformer-lm/blob/ eafc28abdfadfa0732f03a0fc65805c5bfb2ffe7/model_pytorch.py#L45)
* Args:
input_size: the number of input tensor's dimension
num_filters: the number of convolution filter
"""
def __init__(self, input_size, num_filters):
super(PointwiseConv, self).__init__()
self.kernel_size = 1
self.num_filters = num_filters
weight = torch.empty(input_size, num_filters)
nn.init.normal_(weight, std=0.02)
self.weight = nn.Parameter(weight)
self.bias = nn.Parameter(torch.zeros(num_filters))
def forward(self, x):
size_out = x.size()[:-1] + (self.num_filters,)
x = torch.addmm(self.bias, x.contiguous().view(-1, x.size(-1)),
self.weight)
x = x.view(*size_out)
return x
class PositionwiseFeedForwardNew(nn.Module):
"""
Pointwise Feed-Forward Layer
* Args:
input_size: the number of input size
hidden_size: the number of hidden size
* Kwargs:
dropout: the probability of dropout
"""
def __init__(self, input_size, hidden_size, dropout=0.1):
super(PositionwiseFeedForwardNew, self).__init__()
self.pointwise_conv1 = PointwiseConv(input_size=input_size,
num_filters=hidden_size)
self.pointwise_conv2 = PointwiseConv(input_size=hidden_size,
num_filters=input_size)
self.activation_fn = F.relu
self.dropout = nn.Dropout(p=dropout)
def forward(self, input_0):
primals_3 = self.pointwise_conv1.weight
primals_2 = self.pointwise_conv1.bias
primals_5 = self.pointwise_conv2.weight
primals_4 = self.pointwise_conv2.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DongjunLee/claf
|
PositionwiseFeedForward
| false
| 13,588
|
[
"MIT"
] | 225
|
ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
https://github.com/DongjunLee/claf/tree/ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
GraphConvolution
|
import torch
from torch import nn
from torch.nn import init
class GraphConvolution(nn.Module):
def __init__(self, window_size, in_features, out_features):
super(GraphConvolution, self).__init__()
self.weights = nn.Parameter(torch.Tensor(window_size, in_features,
out_features))
self._reset_parameters()
def _reset_parameters(self):
init.xavier_uniform_(self.weights)
def forward(self, adjacency, nodes):
"""
:param adjacency: FloatTensor (batch_size, window_size, node_num, node_num)
:param nodes: FloatTensor (batch_size, window_size, node_num, in_features)
:return output: FloatTensor (batch_size, window_size, node_num, out_features)
"""
batch_size = adjacency.size(0)
window_size, in_features, out_features = self.weights.size()
weights = self.weights.unsqueeze(0).expand(batch_size, window_size,
in_features, out_features)
output = adjacency.matmul(nodes).matmul(weights)
return output
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'window_size': 4, 'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
from torch.nn import init
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 64
x2 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x2, tmp0, 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), (16, 4, 1))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(primals_1, (16, 4, 4), (16, 4,
1), 0), reinterpret_tensor(primals_3, (16, 4, 4), (16, 4, 1), 0
), out=buf0)
del primals_1
del primals_3
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(256)](primals_2, buf1, 256, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf0, reinterpret_tensor(buf1, (16, 4, 4), (16,
4, 1), 0), out=buf2)
del buf1
return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0
), reinterpret_tensor(buf0, (16, 4, 4), (16, 1, 4), 0)
class GraphConvolutionNew(nn.Module):
def __init__(self, window_size, in_features, out_features):
super(GraphConvolutionNew, self).__init__()
self.weights = nn.Parameter(torch.Tensor(window_size, in_features,
out_features))
self._reset_parameters()
def _reset_parameters(self):
init.xavier_uniform_(self.weights)
def forward(self, input_0, input_1):
primals_2 = self.weights
primals_1 = input_0
primals_3 = input_1
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DavidHeSkr/GCN-GAN-pytorch
|
GraphConvolution
| false
| 13,589
|
[
"MIT"
] | 66
|
f8adf82596733464cb63dddf978c244b25aebe46
|
https://github.com/DavidHeSkr/GCN-GAN-pytorch/tree/f8adf82596733464cb63dddf978c244b25aebe46
|
InvHuberLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class InvHuberLoss(nn.Module):
"""Inverse Huber Loss for depth estimation.
The setup is taken from https://arxiv.org/abs/1606.00373
Args:
ignore_index (float): value to ignore in the target
when computing the loss.
"""
def __init__(self, ignore_index=0):
super(InvHuberLoss, self).__init__()
self.ignore_index = ignore_index
def forward(self, x, target):
input = F.relu(x)
diff = input - target
mask = target != self.ignore_index
err = torch.abs(diff * mask.float())
c = 0.2 * torch.max(err)
err2 = (diff ** 2 + c ** 2) / (2.0 * c)
mask_err = err <= c
mask_err2 = err > c
cost = torch.mean(err * mask_err.float() + err2 * mask_err2.float())
return cost
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__to_copy_abs_add_div_gt_le_max_mean_mul_ne_pow_relu_sub_0(
in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp3 = tl.load(in_ptr1 + r0, None)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = tmp2 - tmp3
tmp5 = 0.0
tmp6 = tmp3 != tmp5
tmp7 = tmp6.to(tl.float32)
tmp8 = tmp4 * tmp7
tmp9 = tl_math.abs(tmp8)
tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
tmp12 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp10, 0))
tmp13 = 0.2
tmp14 = tmp12 * tmp13
tmp15 = tmp9 <= tmp14
tmp16 = tmp15.to(tl.float32)
tmp17 = tmp9 * tmp16
tmp18 = tmp4 * tmp4
tmp19 = tmp14 * tmp14
tmp20 = tmp18 + tmp19
tmp21 = 2.0
tmp22 = tmp14 * tmp21
tmp23 = tmp20 / tmp22
tmp24 = tmp9 > tmp14
tmp25 = tmp24.to(tl.float32)
tmp26 = tmp23 * tmp25
tmp27 = tmp17 + tmp26
tmp28 = tl.broadcast_to(tmp27, [RBLOCK])
tmp30 = triton_helpers.promote_to_tensor(tl.sum(tmp28, 0))
tmp31 = 256.0
tmp32 = tmp30 / tmp31
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp32, None)
def call(args):
arg0_1, arg1_1 = 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
buf2 = buf1
del buf1
get_raw_stream(0)
triton_per_fused__to_copy_abs_add_div_gt_le_max_mean_mul_ne_pow_relu_sub_0[
grid(1)](buf2, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf2,
class InvHuberLossNew(nn.Module):
"""Inverse Huber Loss for depth estimation.
The setup is taken from https://arxiv.org/abs/1606.00373
Args:
ignore_index (float): value to ignore in the target
when computing the loss.
"""
def __init__(self, ignore_index=0):
super(InvHuberLossNew, self).__init__()
self.ignore_index = ignore_index
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
DrSleep/DenseTorch
|
InvHuberLoss
| false
| 13,590
|
[
"MIT"
] | 69
|
f90bef075429d763fc08338dea8222d28b0a4516
|
https://github.com/DrSleep/DenseTorch/tree/f90bef075429d763fc08338dea8222d28b0a4516
|
VPReLU
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class VPReLU(nn.Module):
__constants__ = ['inplace']
inplace: 'bool'
def __init__(self, inplace: 'bool'=False):
super(VPReLU, self).__init__()
self.inplace = inplace
def forward(self, input: 'torch.Tensor') ->torch.Tensor:
return F.relu(input, inplace=self.inplace) * 1.7139588594436646
def extra_repr(self) ->str:
inplace_str = 'inplace=True' if self.inplace else ''
return inplace_str
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_mul_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp3 = 1.7139588594436646
tmp4 = tmp2 * tmp3
tl.store(out_ptr0 + x0, tmp4, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mul_relu_0[grid(256)](arg0_1, buf0, 256, XBLOCK=
256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class VPReLUNew(nn.Module):
__constants__ = ['inplace']
inplace: 'bool'
def __init__(self, inplace: 'bool'=False):
super(VPReLUNew, self).__init__()
self.inplace = inplace
def extra_repr(self) ->str:
inplace_str = 'inplace=True' if self.inplace else ''
return inplace_str
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DucNguyen183/nfnet_f5
|
VPReLU
| false
| 13,591
|
[
"Apache-2.0"
] | 133
|
567a1126ff6ea09b33ffa5dacfac9c983fd48713
|
https://github.com/DucNguyen183/nfnet_f5/tree/567a1126ff6ea09b33ffa5dacfac9c983fd48713
|
VPGELU
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class VPGELU(nn.Module):
def forward(self, input: 'torch.Tensor') ->torch.Tensor:
return F.gelu(input) * 1.7015043497085571
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_gelu_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tmp3 = 0.7071067811865476
tmp4 = tmp0 * tmp3
tmp5 = libdevice.erf(tmp4)
tmp6 = 1.0
tmp7 = tmp5 + tmp6
tmp8 = tmp2 * tmp7
tmp9 = 1.7015043497085571
tmp10 = tmp8 * tmp9
tl.store(out_ptr0 + x0, 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_gelu_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=
256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class VPGELUNew(nn.Module):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DucNguyen183/nfnet_f5
|
VPGELU
| false
| 13,592
|
[
"Apache-2.0"
] | 133
|
567a1126ff6ea09b33ffa5dacfac9c983fd48713
|
https://github.com/DucNguyen183/nfnet_f5/tree/567a1126ff6ea09b33ffa5dacfac9c983fd48713
|
ACLoss
|
import torch
import torch.utils.data
class ACLoss(torch.nn.Module):
"""Active Contour loss
http://openaccess.thecvf.com/content_CVPR_2019/papers/Chen_Learning_Active_Contour_Models_for_Medical_Image_Segmentation_CVPR_2019_paper.pdf
Supports 2D and 3D data, as long as all spatial dimensions have the same
size and there are only two output channels.
Modifications:
- Using mean instead of sum for reductions to avoid size dependency.
- Instead of the proposed λ loss component weighting (which leads to
exploding loss magnitudes for high λ values), a relative weight
``region_weight`` is used to balance the components:
``ACLoss = (1 - region_weight) * length_term + region_weight * region_term``
"""
def __init__(self, num_classes: 'int', region_weight: 'float'=0.5):
assert 0.0 <= region_weight <= 1.0, 'region_weight must be between 0 and 1'
self.num_classes = num_classes
self.region_weight = region_weight
super().__init__()
@staticmethod
def get_length(output):
if output.ndim == 4:
dy = output[:, :, 1:, :] - output[:, :, :-1, :]
dx = output[:, :, :, 1:] - output[:, :, :, :-1]
dy = dy[:, :, 1:, :-2] ** 2
dx = dx[:, :, :-2, 1:] ** 2
delta_pred = torch.abs(dy + dx)
elif output.ndim == 5:
assert output.shape[3] == output.shape[4
], 'All spatial dims must have the same size'
dz = output[:, :, 1:, :, :] - output[:, :, :-1, :, :]
dy = output[:, :, :, 1:, :] - output[:, :, :, :-1, :]
dx = output[:, :, :, :, 1:] - output[:, :, :, :, :-1]
dz = dz[:, :, 1:, :-2, :-2] ** 2
dy = dy[:, :, :-2, 1:, :-2] ** 2
dx = dx[:, :, :-2, :-2, 1:] ** 2
delta_pred = torch.abs(dz + dy + dx)
length = torch.mean(torch.sqrt(delta_pred + 1e-06))
return length
@staticmethod
def get_region(output, target):
region_in = torch.mean(output * (target - 1.0) ** 2.0)
region_out = torch.mean((1 - output) * target ** 2.0)
return region_in + region_out
def forward(self, output, target):
assert output.shape[2] == output.shape[3
], 'All spatial dims must have the same size'
if target.ndim == output.ndim - 1:
target = torch.nn.functional.one_hot(target, self.num_classes
).transpose(1, -1)
length_term = self.get_length(output
) if self.region_weight < 1.0 else 0.0
region_term = self.get_region(output, target
) if self.region_weight > 0.0 else 0.0
loss = (1 - self.region_weight
) * length_term + self.region_weight * region_term
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'num_classes': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_abs_add_mean_pow_sqrt_0(in_ptr0, out_ptr0, xnumel,
rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex % 2
r1 = rindex // 2 % 2
r2 = rindex // 4
tmp0 = tl.load(in_ptr0 + (8 + r0 + 4 * r1 + 16 * r2), None)
tmp1 = tl.load(in_ptr0 + (4 + r0 + 4 * r1 + 16 * r2), None)
tmp4 = tl.load(in_ptr0 + (2 + r0 + 4 * r1 + 16 * r2), None)
tmp5 = tl.load(in_ptr0 + (1 + r0 + 4 * r1 + 16 * r2), None)
tmp2 = tmp0 - tmp1
tmp3 = tmp2 * tmp2
tmp6 = tmp4 - tmp5
tmp7 = tmp6 * tmp6
tmp8 = tmp3 + tmp7
tmp9 = tl_math.abs(tmp8)
tmp10 = 1e-06
tmp11 = tmp9 + tmp10
tmp12 = libdevice.sqrt(tmp11)
tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
tmp15 = tl.sum(tmp13, 1)[:, None]
tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp15, None)
@triton.jit
def triton_per_fused_abs_add_mean_mul_pow_rsub_sqrt_sub_1(in_out_ptr0,
in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp15 = tl.load(in_out_ptr0 + 0)
tmp16 = tl.broadcast_to(tmp15, [1])
tmp2 = 1.0
tmp3 = tmp1 - tmp2
tmp4 = tmp3 * tmp3
tmp5 = tmp0 * tmp4
tmp6 = tl.broadcast_to(tmp5, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = tmp2 - tmp0
tmp10 = tmp1 * tmp1
tmp11 = tmp9 * tmp10
tmp12 = tl.broadcast_to(tmp11, [RBLOCK])
tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0))
tmp17 = 64.0
tmp18 = tmp16 / tmp17
tmp19 = 0.5
tmp20 = tmp18 * tmp19
tmp21 = 256.0
tmp22 = tmp8 / tmp21
tmp23 = tmp14 / tmp21
tmp24 = tmp22 + tmp23
tmp25 = tmp24 * tmp19
tmp26 = tmp20 + tmp25
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp26, 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_add_mean_pow_sqrt_0[grid(1)](arg0_1, buf0, 1,
64, XBLOCK=1, num_warps=2, num_stages=1)
buf3 = buf0
del buf0
triton_per_fused_abs_add_mean_mul_pow_rsub_sqrt_sub_1[grid(1)](buf3,
arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf3,
class ACLossNew(torch.nn.Module):
"""Active Contour loss
http://openaccess.thecvf.com/content_CVPR_2019/papers/Chen_Learning_Active_Contour_Models_for_Medical_Image_Segmentation_CVPR_2019_paper.pdf
Supports 2D and 3D data, as long as all spatial dimensions have the same
size and there are only two output channels.
Modifications:
- Using mean instead of sum for reductions to avoid size dependency.
- Instead of the proposed λ loss component weighting (which leads to
exploding loss magnitudes for high λ values), a relative weight
``region_weight`` is used to balance the components:
``ACLoss = (1 - region_weight) * length_term + region_weight * region_term``
"""
def __init__(self, num_classes: 'int', region_weight: 'float'=0.5):
assert 0.0 <= region_weight <= 1.0, 'region_weight must be between 0 and 1'
self.num_classes = num_classes
self.region_weight = region_weight
super().__init__()
@staticmethod
def get_length(output):
if output.ndim == 4:
dy = output[:, :, 1:, :] - output[:, :, :-1, :]
dx = output[:, :, :, 1:] - output[:, :, :, :-1]
dy = dy[:, :, 1:, :-2] ** 2
dx = dx[:, :, :-2, 1:] ** 2
delta_pred = torch.abs(dy + dx)
elif output.ndim == 5:
assert output.shape[3] == output.shape[4
], 'All spatial dims must have the same size'
dz = output[:, :, 1:, :, :] - output[:, :, :-1, :, :]
dy = output[:, :, :, 1:, :] - output[:, :, :, :-1, :]
dx = output[:, :, :, :, 1:] - output[:, :, :, :, :-1]
dz = dz[:, :, 1:, :-2, :-2] ** 2
dy = dy[:, :, :-2, 1:, :-2] ** 2
dx = dx[:, :, :-2, :-2, 1:] ** 2
delta_pred = torch.abs(dz + dy + dx)
length = torch.mean(torch.sqrt(delta_pred + 1e-06))
return length
@staticmethod
def get_region(output, target):
region_in = torch.mean(output * (target - 1.0) ** 2.0)
region_out = torch.mean((1 - output) * target ** 2.0)
return region_in + region_out
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ELEKTRONN/elektronn3
|
ACLoss
| false
| 13,593
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
Argmax
|
import torch
from torch import nn
import torch.utils.data
class Argmax(nn.Module):
def __init__(self, dim=1, unsqueeze=True):
super().__init__()
self.dim = dim
self.unsqueeze = unsqueeze
def forward(self, x):
argmax = torch.argmax(x, self.dim)
if self.unsqueeze:
argmax.unsqueeze_(1)
return argmax
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
import torch.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_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 % 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)
tl.store(out_ptr0 + x2, tmp46, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.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
return reinterpret_tensor(buf0, (4, 1, 4, 4), (16, 16, 4, 1), 0),
class ArgmaxNew(nn.Module):
def __init__(self, dim=1, unsqueeze=True):
super().__init__()
self.dim = dim
self.unsqueeze = unsqueeze
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ELEKTRONN/elektronn3
|
Argmax
| false
| 13,594
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
LayerNorm
|
import torch
import torch.utils.data
import torch.nn as nn
class LayerNorm(nn.Module):
def __init__(self, features, eps=1e-06, gamma=1.0, beta=0.0, learnable=
False):
super(LayerNorm, self).__init__()
if learnable:
self.gamma = nn.Parameter(torch.ones(features))
self.beta = nn.Parameter(torch.zeros(features))
else:
self.gamma = gamma
self.beta = beta
self.eps = eps
def forward(self, x):
x_size = x.size()
mean = x.view(x_size[0], x_size[1], x_size[2] * x_size[3]).mean(2
).view(x_size[0], x_size[1], 1, 1).repeat(1, 1, x_size[2],
x_size[3])
std = x.view(x_size[0], x_size[1], x_size[2] * x_size[3]).std(2).view(
x_size[0], x_size[1], 1, 1).repeat(1, 1, x_size[2], x_size[3])
return self.gamma * (x - mean) / (std + self.eps) + self.beta
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.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_per_fused_add_div_mean_mul_repeat_std_sub_0(in_ptr0, out_ptr2,
xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tmp6 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
tmp8 = tl.where(xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp1 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.where(xmask, tmp15, 0)
tmp18 = tl.sum(tmp17, 1)[:, None]
tmp19 = 16.0
tmp20 = tmp4 / tmp19
tmp21 = tmp0 - tmp20
tmp22 = 1.0
tmp23 = tmp21 * tmp22
tmp24 = 15.0
tmp25 = tmp18 / tmp24
tmp26 = libdevice.sqrt(tmp25)
tmp27 = 1e-06
tmp28 = tmp26 + tmp27
tmp29 = tmp23 / tmp28
tmp30 = 0.0
tmp31 = tmp29 + tmp30
tl.store(out_ptr2 + (r1 + 16 * x0), tmp31, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_per_fused_add_div_mean_mul_repeat_std_sub_0[grid(16)](arg0_1,
buf4, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
return buf4,
class LayerNormNew(nn.Module):
def __init__(self, features, eps=1e-06, gamma=1.0, beta=0.0, learnable=
False):
super(LayerNormNew, self).__init__()
if learnable:
self.gamma = nn.Parameter(torch.ones(features))
self.beta = nn.Parameter(torch.zeros(features))
else:
self.gamma = gamma
self.beta = beta
self.eps = eps
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
E18301194/DepthAwareCNN
|
LayerNorm
| false
| 13,595
|
[
"MIT"
] | 278
|
8ae98f7f18b69f79e7df03397dec2543d3d0c8eb
|
https://github.com/E18301194/DepthAwareCNN/tree/8ae98f7f18b69f79e7df03397dec2543d3d0c8eb
|
GAPTripletMarginLoss
|
import torch
from torch import nn
import torch.utils.data
import torch.nn.functional as F
from torch.functional import F
def global_average_pooling(inp: 'torch.Tensor') ->torch.Tensor:
if inp.ndim == 5:
return F.adaptive_avg_pool3d(inp, 1)
elif inp.ndim == 4:
return F.adaptive_avg_pool2d(inp, 1)
else:
raise NotImplementedError
class GAPTripletMarginLoss(nn.TripletMarginLoss):
"""Same as ``torch.nn.TripletMarginLoss``, but applies global average
pooling to anchor, positive and negative tensors before calculating the
loss."""
def forward(self, anchor: 'torch.Tensor', positive: 'torch.Tensor',
negative: 'torch.Tensor') ->torch.Tensor:
return super().forward(global_average_pooling(anchor),
global_average_pooling(positive), global_average_pooling(negative))
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
[4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
from torch import nn
import torch.utils.data
import torch.nn.functional as F
from torch.functional import 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_mean_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tl.store(out_ptr0 + x0, tmp4, xmask)
@triton.jit
def triton_per_fused_add_clamp_min_mean_norm_sub_1(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp3 = tl.load(in_ptr1 + r0, None)
tmp12 = tl.load(in_ptr2 + r0, None)
tmp1 = 16.0
tmp2 = tmp0 / tmp1
tmp4 = tmp3 / tmp1
tmp5 = tmp2 - tmp4
tmp6 = 1e-06
tmp7 = tmp5 + tmp6
tmp8 = tmp7 * tmp7
tmp9 = libdevice.sqrt(tmp8)
tmp10 = 1.0
tmp11 = tmp9 + tmp10
tmp13 = tmp12 / tmp1
tmp14 = tmp2 - tmp13
tmp15 = tmp14 + tmp6
tmp16 = tmp15 * tmp15
tmp17 = libdevice.sqrt(tmp16)
tmp18 = tmp11 - tmp17
tmp19 = 0.0
tmp20 = triton_helpers.maximum(tmp18, tmp19)
tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK])
tmp23 = tl.sum(tmp21, 1)[:, None]
tmp24 = tmp23 / tmp1
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp24, None)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
get_raw_stream(0)
triton_per_fused_mean_0[grid(16)](arg0_1, buf0, 16, 16, XBLOCK=1,
num_warps=2, num_stages=1)
del arg0_1
buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
triton_per_fused_mean_0[grid(16)](arg1_1, buf1, 16, 16, XBLOCK=1,
num_warps=2, num_stages=1)
del arg1_1
buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
triton_per_fused_mean_0[grid(16)](arg2_1, buf2, 16, 16, XBLOCK=1,
num_warps=2, num_stages=1)
del arg2_1
buf3 = empty_strided_cuda((), (), torch.float32)
buf4 = buf3
del buf3
triton_per_fused_add_clamp_min_mean_norm_sub_1[grid(1)](buf4, buf0,
buf1, buf2, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
del buf0
del buf1
del buf2
return buf4,
def global_average_pooling(inp: 'torch.Tensor') ->torch.Tensor:
if inp.ndim == 5:
return F.adaptive_avg_pool3d(inp, 1)
elif inp.ndim == 4:
return F.adaptive_avg_pool2d(inp, 1)
else:
raise NotImplementedError
class GAPTripletMarginLossNew(nn.TripletMarginLoss):
"""Same as ``torch.nn.TripletMarginLoss``, but applies global average
pooling to anchor, positive and negative tensors before calculating the
loss."""
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]
|
ELEKTRONN/elektronn3
|
GAPTripletMarginLoss
| false
| 13,596
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
TransformerEncoderLayer
|
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.utils.data
class Linear(nn.Linear):
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
super(Linear, self).__init__(in_dim, out_dim, bias)
nn.init.xavier_uniform_(self.weight, gain=nn.init.calculate_gain(
w_init_gain))
class TransformerEncoderLayer(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1,
activation='relu'):
super(TransformerEncoderLayer, self).__init__()
self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
self.linear1 = Linear(d_model, dim_feedforward, w_init_gain=activation)
self.linear2 = Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, src, src_mask=None, src_key_padding_mask=None):
src2, enc_align = self.self_attn(src, src, src, attn_mask=src_mask,
key_padding_mask=src_key_padding_mask)
src = src + self.dropout(src2)
src = self.norm1(src)
src2 = self.linear2(self.dropout(F.relu(self.linear1(src))))
src = src + self.dropout(src2)
src = self.norm2(src)
return src, enc_align
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {'d_model': 4, 'nhead': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 1.0
tmp4 = tmp2 * tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 4
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x1 = xindex
y0 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_mean_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.load(in_ptr0 + (16 + x0), xmask)
tmp3 = tl.load(in_ptr0 + (32 + x0), xmask)
tmp5 = tl.load(in_ptr0 + (48 + x0), xmask)
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_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 + tmp8
tmp10 = tmp6 + tmp9
tmp13 = tmp11 + tmp12
tmp14 = tmp10 + tmp13
tmp15 = 4.0
tmp16 = tmp14 / tmp15
tmp17 = tmp2 - tmp16
tmp18 = tmp17 * tmp17
tmp19 = tmp5 - tmp16
tmp20 = tmp19 * tmp19
tmp21 = tmp18 + tmp20
tmp22 = tmp9 - tmp16
tmp23 = tmp22 * tmp22
tmp24 = tmp21 + tmp23
tmp25 = tmp13 - tmp16
tmp26 = tmp25 * tmp25
tmp27 = tmp24 + tmp26
tmp28 = tmp27 / tmp15
tl.store(out_ptr0 + x0, tmp16, xmask)
tl.store(out_ptr1 + x0, tmp28, xmask)
@triton.jit
def triton_poi_fused_add_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 - tmp3
tmp6 = 1e-05
tmp7 = tmp5 + tmp6
tmp8 = libdevice.rsqrt(tmp7)
tmp9 = tmp4 * tmp8
tmp11 = tmp9 * tmp10
tmp13 = tmp11 + tmp12
tl.store(out_ptr0 + x2, tmp13, xmask)
@triton.jit
def triton_poi_fused_relu_7(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 2048
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
@triton.jit
def triton_poi_fused_add_8(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_native_layer_norm_9(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp7 = 4.0
tmp8 = tmp6 / tmp7
tmp9 = tmp0 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tmp1 - tmp8
tmp12 = tmp11 * tmp11
tmp13 = tmp10 + tmp12
tmp14 = tmp3 - tmp8
tmp15 = tmp14 * tmp14
tmp16 = tmp13 + tmp15
tmp17 = tmp5 - tmp8
tmp18 = tmp17 * tmp17
tmp19 = tmp16 + tmp18
tmp20 = tmp19 / tmp7
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tl.store(out_ptr0 + x0, tmp8, xmask)
tl.store(out_ptr1 + x0, tmp23, xmask)
@triton.jit
def triton_poi_fused_native_layer_norm_10(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = tmp2 * tmp3
tmp6 = tmp4 * tmp5
tmp8 = tmp6 + tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (12, 4), (4, 1))
assert_size_stride(primals_3, (12,), (1,))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4,), (1,))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (2048, 4), (4, 1))
assert_size_stride(primals_9, (2048,), (1,))
assert_size_stride(primals_10, (4, 2048), (2048, 1))
assert_size_stride(primals_11, (4,), (1,))
assert_size_stride(primals_12, (4,), (1,))
assert_size_stride(primals_13, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4),
(1, 4), 0), out=buf0)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 4),
primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 16),
alpha=1, beta=1, out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 8),
primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 32),
alpha=1, beta=1, out=buf2)
del primals_2
buf3 = reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 16), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](buf3, primals_3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_3
buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf3, reinterpret_tensor(buf1, (4, 1, 4), (1, 1,
4), 0), out=buf4)
buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf6 = buf4
del buf4
triton_poi_fused__softmax_2[grid(64)](buf5, buf6, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf5
buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 1), (1, 4,
1), 0), out=buf7)
buf8 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
triton_poi_fused_clone_3[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4,
YBLOCK=4, num_warps=1, num_stages=1)
buf9 = reinterpret_tensor(buf7, (4, 4), (4, 1), 0)
del buf7
extern_kernels.addmm(primals_5, reinterpret_tensor(buf8, (4, 4), (4,
1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha
=1, beta=1, out=buf9)
del primals_5
buf10 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_mean_4[grid(16)](buf6, buf10, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf11 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
buf12 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
triton_poi_fused_add_native_layer_norm_5[grid(4)](primals_1, buf9,
buf11, buf12, 4, XBLOCK=4, num_warps=1, num_stages=1)
buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_native_layer_norm_6[grid(16)](primals_1, buf9,
buf11, buf12, primals_6, primals_7, buf13, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_7
buf14 = empty_strided_cuda((4, 2048), (2048, 1), torch.float32)
extern_kernels.mm(buf13, reinterpret_tensor(primals_8, (4, 2048), (
1, 4), 0), out=buf14)
buf15 = buf14
del buf14
triton_poi_fused_relu_7[grid(8192)](buf15, primals_9, 8192, XBLOCK=
256, num_warps=4, num_stages=1)
del primals_9
buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(buf15, reinterpret_tensor(primals_10, (2048, 4),
(1, 2048), 0), out=buf16)
buf17 = buf16
del buf16
triton_poi_fused_add_8[grid(16)](buf17, buf13, primals_11, 16,
XBLOCK=16, num_warps=1, num_stages=1)
del primals_11
buf18 = buf12
del buf12
buf19 = buf11
del buf11
triton_poi_fused_native_layer_norm_9[grid(4)](buf17, buf18, buf19,
4, XBLOCK=4, num_warps=1, num_stages=1)
buf20 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_native_layer_norm_10[grid(16)](buf17, buf18, buf19,
primals_12, primals_13, buf20, 16, XBLOCK=16, num_warps=1,
num_stages=1)
del buf18
del buf19
del primals_13
return (buf20, reinterpret_tensor(buf10, (4, 4), (4, 1), 0), primals_1,
primals_6, primals_12, buf6, reinterpret_tensor(buf8, (4, 4), (4, 1
), 0), buf9, buf13, buf15, buf17, primals_10, primals_8, primals_4,
reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf3, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0))
class Linear(nn.Linear):
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
super(Linear, self).__init__(in_dim, out_dim, bias)
nn.init.xavier_uniform_(self.weight, gain=nn.init.calculate_gain(
w_init_gain))
class TransformerEncoderLayerNew(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1,
activation='relu'):
super(TransformerEncoderLayerNew, self).__init__()
self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
self.linear1 = Linear(d_model, dim_feedforward, w_init_gain=activation)
self.linear2 = Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, input_0):
primals_2 = self.self_attn.in_proj_weight
primals_3 = self.self_attn.in_proj_bias
primals_1 = self.self_attn.out_proj.weight
primals_5 = self.self_attn.out_proj.bias
primals_8 = self.linear1.weight
primals_9 = self.linear1.bias
primals_10 = self.linear2.weight
primals_6 = self.linear2.bias
primals_7 = self.norm1.weight
primals_11 = self.norm1.bias
primals_12 = self.norm2.weight
primals_13 = self.norm2.bias
primals_4 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13])
return output[0], output[1]
|
Deepest-Project/AlignTTS
|
TransformerEncoderLayer
| false
| 13,597
|
[
"MIT"
] | 70
|
ed9c29d845f65ceb44c87f293b2919b9bbc6a6de
|
https://github.com/Deepest-Project/AlignTTS/tree/ed9c29d845f65ceb44c87f293b2919b9bbc6a6de
|
TransformerDecoderLayer
|
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.utils.data
class Linear(nn.Linear):
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
super(Linear, self).__init__(in_dim, out_dim, bias)
nn.init.xavier_uniform_(self.weight, gain=nn.init.calculate_gain(
w_init_gain))
class TransformerDecoderLayer(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1,
activation='relu'):
super(TransformerDecoderLayer, self).__init__()
self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout
=dropout)
self.linear1 = Linear(d_model, dim_feedforward, w_init_gain=activation)
self.linear2 = Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, tgt, memory, tgt_mask=None, memory_mask=None,
tgt_key_padding_mask=None, memory_key_padding_mask=None):
tgt2, dec_align = self.self_attn(tgt, tgt, tgt, attn_mask=tgt_mask,
key_padding_mask=tgt_key_padding_mask)
tgt = tgt + self.dropout(tgt2)
tgt = self.norm1(tgt)
tgt2, enc_dec_align = self.multihead_attn(tgt, memory, memory,
attn_mask=memory_mask, key_padding_mask=memory_key_padding_mask)
tgt = tgt + self.dropout(tgt2)
tgt = self.norm2(tgt)
tgt2 = self.linear2(self.dropout(F.relu(self.linear1(tgt))))
tgt = tgt + self.dropout(tgt2)
tgt = self.norm3(tgt)
return tgt, dec_align, enc_dec_align
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'d_model': 4, 'nhead': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 1.0
tmp4 = tmp2 * tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 4
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x1 = xindex
y0 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_mean_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.load(in_ptr0 + (16 + x0), xmask)
tmp3 = tl.load(in_ptr0 + (32 + x0), xmask)
tmp5 = tl.load(in_ptr0 + (48 + x0), xmask)
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_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp2 = tmp0 + tmp1
tmp5 = tmp3 + tmp4
tmp6 = tmp2 + tmp5
tmp9 = tmp7 + tmp8
tmp10 = tmp6 + tmp9
tmp13 = tmp11 + tmp12
tmp14 = tmp10 + tmp13
tmp15 = 4.0
tmp16 = tmp14 / tmp15
tmp17 = tmp2 - tmp16
tmp18 = tmp17 * tmp17
tmp19 = tmp5 - tmp16
tmp20 = tmp19 * tmp19
tmp21 = tmp18 + tmp20
tmp22 = tmp9 - tmp16
tmp23 = tmp22 * tmp22
tmp24 = tmp21 + tmp23
tmp25 = tmp13 - tmp16
tmp26 = tmp25 * tmp25
tmp27 = tmp24 + tmp26
tmp28 = tmp27 / tmp15
tl.store(out_ptr0 + x0, tmp16, xmask)
tl.store(out_ptr1 + x0, tmp28, xmask)
@triton.jit
def triton_poi_fused_add_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2,
in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 - tmp3
tmp6 = 1e-05
tmp7 = tmp5 + tmp6
tmp8 = libdevice.rsqrt(tmp7)
tmp9 = tmp4 * tmp8
tmp11 = tmp9 * tmp10
tmp13 = tmp11 + tmp12
tl.store(out_ptr0 + x2, tmp13, xmask)
@triton.jit
def triton_poi_fused_add_7(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_native_layer_norm_8(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp7 = 4.0
tmp8 = tmp6 / tmp7
tmp9 = tmp0 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tmp1 - tmp8
tmp12 = tmp11 * tmp11
tmp13 = tmp10 + tmp12
tmp14 = tmp3 - tmp8
tmp15 = tmp14 * tmp14
tmp16 = tmp13 + tmp15
tmp17 = tmp5 - tmp8
tmp18 = tmp17 * tmp17
tmp19 = tmp16 + tmp18
tmp20 = tmp19 / tmp7
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tl.store(out_ptr0 + x0, tmp8, xmask)
tl.store(out_ptr1 + x0, tmp23, xmask)
@triton.jit
def triton_poi_fused_native_layer_norm_9(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = tmp2 * tmp3
tmp6 = tmp4 * tmp5
tmp8 = tmp6 + tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_relu_10(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 2048
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, None)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17,
primals_18, primals_19, primals_20) = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (12, 4), (4, 1))
assert_size_stride(primals_3, (12,), (1,))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4,), (1,))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4, 4), (4, 1))
assert_size_stride(primals_9, (12, 4), (4, 1))
assert_size_stride(primals_10, (12,), (1,))
assert_size_stride(primals_11, (4, 4), (4, 1))
assert_size_stride(primals_12, (4,), (1,))
assert_size_stride(primals_13, (4,), (1,))
assert_size_stride(primals_14, (4,), (1,))
assert_size_stride(primals_15, (2048, 4), (4, 1))
assert_size_stride(primals_16, (2048,), (1,))
assert_size_stride(primals_17, (4, 2048), (2048, 1))
assert_size_stride(primals_18, (4,), (1,))
assert_size_stride(primals_19, (4,), (1,))
assert_size_stride(primals_20, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4),
(1, 4), 0), out=buf0)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 4),
primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 16),
alpha=1, beta=1, out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 8),
primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 32),
alpha=1, beta=1, out=buf2)
del primals_2
buf3 = reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 16), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](buf3, primals_3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_3
buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf3, reinterpret_tensor(buf1, (4, 1, 4), (1, 1,
4), 0), out=buf4)
buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf6 = buf4
del buf4
triton_poi_fused__softmax_2[grid(64)](buf5, buf6, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 1), (1, 4,
1), 0), out=buf7)
buf8 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
triton_poi_fused_clone_3[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4,
YBLOCK=4, num_warps=1, num_stages=1)
buf9 = reinterpret_tensor(buf7, (4, 4), (4, 1), 0)
del buf7
extern_kernels.addmm(primals_5, reinterpret_tensor(buf8, (4, 4), (4,
1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha
=1, beta=1, out=buf9)
del primals_5
buf10 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_mean_4[grid(16)](buf6, buf10, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf11 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
buf12 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
triton_poi_fused_add_native_layer_norm_5[grid(4)](primals_1, buf9,
buf11, buf12, 4, XBLOCK=4, num_warps=1, num_stages=1)
buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_native_layer_norm_6[grid(16)](primals_1, buf9,
buf11, buf12, primals_6, primals_7, buf13, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_7
buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(buf13, reinterpret_tensor(primals_9, (4, 4), (1,
4), 0), out=buf14)
buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_10, (4,), (1,), 4),
primals_8, reinterpret_tensor(primals_9, (4, 4), (1, 4), 16),
alpha=1, beta=1, out=buf15)
buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.addmm(reinterpret_tensor(primals_10, (4,), (1,), 8),
primals_8, reinterpret_tensor(primals_9, (4, 4), (1, 4), 32),
alpha=1, beta=1, out=buf16)
buf17 = reinterpret_tensor(buf14, (4, 4, 1), (1, 4, 16), 0)
del buf14
triton_poi_fused_mul_0[grid(16)](buf17, primals_10, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_10
buf18 = buf5
del buf5
extern_kernels.bmm(buf17, reinterpret_tensor(buf15, (4, 1, 4), (1,
1, 4), 0), out=buf18)
buf19 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(64)](buf18, buf19, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf20 = buf18
del buf18
triton_poi_fused__softmax_2[grid(64)](buf19, buf20, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del buf19
buf21 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
extern_kernels.bmm(buf20, reinterpret_tensor(buf16, (4, 4, 1), (1,
4, 1), 0), out=buf21)
buf22 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
triton_poi_fused_clone_3[grid(4, 4)](buf21, buf22, 4, 4, XBLOCK=4,
YBLOCK=4, num_warps=1, num_stages=1)
buf23 = reinterpret_tensor(buf21, (4, 4), (4, 1), 0)
del buf21
extern_kernels.mm(reinterpret_tensor(buf22, (4, 4), (4, 1), 0),
reinterpret_tensor(primals_11, (4, 4), (1, 4), 0), out=buf23)
buf24 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32)
triton_poi_fused_mean_4[grid(16)](buf20, buf24, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf25 = buf23
del buf23
triton_poi_fused_add_7[grid(16)](buf25, buf13, primals_12, 16,
XBLOCK=16, num_warps=1, num_stages=1)
del primals_12
buf26 = buf12
del buf12
buf27 = buf11
del buf11
triton_poi_fused_native_layer_norm_8[grid(4)](buf25, buf26, buf27,
4, XBLOCK=4, num_warps=1, num_stages=1)
buf28 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_native_layer_norm_9[grid(16)](buf25, buf26, buf27,
primals_13, primals_14, buf28, 16, XBLOCK=16, num_warps=1,
num_stages=1)
del primals_14
buf29 = empty_strided_cuda((4, 2048), (2048, 1), torch.float32)
extern_kernels.mm(buf28, reinterpret_tensor(primals_15, (4, 2048),
(1, 4), 0), out=buf29)
buf30 = buf29
del buf29
triton_poi_fused_relu_10[grid(8192)](buf30, primals_16, 8192,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_16
buf31 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(buf30, reinterpret_tensor(primals_17, (2048, 4),
(1, 2048), 0), out=buf31)
buf32 = buf31
del buf31
triton_poi_fused_add_7[grid(16)](buf32, buf28, primals_18, 16,
XBLOCK=16, num_warps=1, num_stages=1)
del primals_18
buf33 = buf27
del buf27
buf34 = buf26
del buf26
triton_poi_fused_native_layer_norm_8[grid(4)](buf32, buf33, buf34,
4, XBLOCK=4, num_warps=1, num_stages=1)
buf35 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_native_layer_norm_9[grid(16)](buf32, buf33, buf34,
primals_19, primals_20, buf35, 16, XBLOCK=16, num_warps=1,
num_stages=1)
del buf33
del buf34
del primals_20
return (buf35, reinterpret_tensor(buf10, (4, 4), (4, 1), 0),
reinterpret_tensor(buf24, (4, 4), (4, 1), 0), primals_1, primals_6,
primals_13, primals_19, buf6, reinterpret_tensor(buf8, (4, 4), (4,
1), 0), buf9, buf13, primals_8, buf20, reinterpret_tensor(buf22, (4,
4), (4, 1), 0), buf25, buf28, buf30, buf32, primals_17, primals_15,
primals_11, reinterpret_tensor(buf16, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf17, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf15, (4, 4, 1), (1, 4, 1), 0),
reinterpret_tensor(primals_9, (4, 4), (4, 1), 0), primals_4,
reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf3, (4, 1, 4), (1, 1, 4), 0),
reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0))
class Linear(nn.Linear):
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
super(Linear, self).__init__(in_dim, out_dim, bias)
nn.init.xavier_uniform_(self.weight, gain=nn.init.calculate_gain(
w_init_gain))
class TransformerDecoderLayerNew(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1,
activation='relu'):
super(TransformerDecoderLayerNew, self).__init__()
self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout
=dropout)
self.linear1 = Linear(d_model, dim_feedforward, w_init_gain=activation)
self.linear2 = Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, input_0, input_1):
primals_2 = self.self_attn.in_proj_weight
primals_3 = self.self_attn.in_proj_bias
primals_1 = self.self_attn.out_proj.weight
primals_5 = self.self_attn.out_proj.bias
primals_9 = self.multihead_attn.in_proj_weight
primals_10 = self.multihead_attn.in_proj_bias
primals_4 = self.multihead_attn.out_proj.weight
primals_6 = self.multihead_attn.out_proj.bias
primals_15 = self.linear1.weight
primals_16 = self.linear1.bias
primals_17 = self.linear2.weight
primals_7 = self.linear2.bias
primals_12 = self.norm1.weight
primals_13 = self.norm1.bias
primals_14 = self.norm2.weight
primals_18 = self.norm2.bias
primals_19 = self.norm3.weight
primals_20 = self.norm3.bias
primals_8 = 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, primals_13, primals_14,
primals_15, primals_16, primals_17, primals_18, primals_19,
primals_20])
return output[0], output[1], output[2]
|
Deepest-Project/AlignTTS
|
TransformerDecoderLayer
| false
| 13,598
|
[
"MIT"
] | 70
|
ed9c29d845f65ceb44c87f293b2919b9bbc6a6de
|
https://github.com/Deepest-Project/AlignTTS/tree/ed9c29d845f65ceb44c87f293b2919b9bbc6a6de
|
DQN
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class DQN(nn.Module):
"""
Deep neural network with represents an agent.
"""
def __init__(self, input_size, num_actions):
super(DQN, self).__init__()
self.linear1 = nn.Linear(input_size, 50)
self.head = nn.Linear(50, num_actions)
def forward(self, x):
x = F.leaky_relu(self.linear1(x))
return self.head(x)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'num_actions': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 3200
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 50
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.01
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tl.store(out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr1 + x2, tmp7, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (50, 4), (4, 1))
assert_size_stride(primals_2, (50,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 50), (50, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool)
buf2 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.
float32)
get_raw_stream(0)
triton_poi_fused_leaky_relu_0[grid(3200)](buf0, primals_2, buf1,
buf2, 3200, XBLOCK=128, num_warps=4, num_stages=1)
del buf0
del primals_2
buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 50),
(50, 1), 0), reinterpret_tensor(primals_4, (50, 4), (1, 50), 0),
alpha=1, beta=1, out=buf3)
del primals_5
return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), buf1, reinterpret_tensor(buf2, (64, 50), (50, 1), 0), primals_4
class DQNNew(nn.Module):
"""
Deep neural network with represents an agent.
"""
def __init__(self, input_size, num_actions):
super(DQNNew, self).__init__()
self.linear1 = nn.Linear(input_size, 50)
self.head = nn.Linear(50, num_actions)
def forward(self, input_0):
primals_1 = self.linear1.weight
primals_2 = self.linear1.bias
primals_4 = self.head.weight
primals_5 = self.head.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
Dookas/Robust-Multitask-RL
|
DQN
| false
| 13,599
|
[
"MIT"
] | 106
|
7970e20cbdf91703c88edcb84568d7354e2525bc
|
https://github.com/Dookas/Robust-Multitask-RL/tree/7970e20cbdf91703c88edcb84568d7354e2525bc
|
SeqAttnMatch
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class SeqAttnMatch(nn.Module):
"""
Given sequences X and Y, match sequence Y to each element in X.
* o_i = sum(alpha_j * y_j) for i in X
* alpha_j = softmax(y_j * x_i)
"""
def __init__(self, embed_dim, identity=False):
super(SeqAttnMatch, self).__init__()
if not identity:
self.linear = nn.Linear(embed_dim, embed_dim)
else:
self.linear = None
def forward(self, x, y, y_mask):
if self.linear:
x_proj = self.linear(x.view(-1, x.size(2))).view(x.size())
x_proj = F.relu(x_proj)
y_proj = self.linear(y.view(-1, y.size(2))).view(y.size())
y_proj = F.relu(y_proj)
else:
x_proj = x
y_proj = y
scores = x_proj.bmm(y_proj.transpose(2, 1))
y_mask = y_mask.unsqueeze(1).expand(scores.size())
scores = scores.masked_fill(y_mask == 0, -1e+30)
alpha_flat = F.softmax(scores.view(-1, y.size(1)), -1)
alpha = alpha_flat.view(-1, x.size(1), y.size(1))
matched_seq = alpha.bmm(y)
return matched_seq
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'embed_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_out_ptr1,
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')
tmp5 = tl.load(in_out_ptr1 + x2, xmask)
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = tmp5 + tmp1
tmp7 = triton_helpers.maximum(tmp3, tmp6)
tmp8 = 0.0
tmp9 = tmp7 <= tmp8
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(in_out_ptr1 + x2, tmp7, xmask)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 4 * (x0 // 4), xmask, eviction_policy='evict_last'
)
tmp3 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (1 + 4 * (x0 // 4)), xmask, eviction_policy=
'evict_last')
tmp8 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr0 + (2 + 4 * (x0 // 4)), xmask, eviction_policy=
'evict_last')
tmp13 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp16 = tl.load(in_ptr0 + (3 + 4 * (x0 // 4)), xmask, eviction_policy=
'evict_last')
tmp18 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp1 = 0.0
tmp2 = tmp0 == tmp1
tmp4 = -1.0000000150474662e+30
tmp5 = tl.where(tmp2, tmp4, tmp3)
tmp7 = tmp6 == tmp1
tmp9 = tl.where(tmp7, tmp4, tmp8)
tmp10 = triton_helpers.maximum(tmp5, tmp9)
tmp12 = tmp11 == tmp1
tmp14 = tl.where(tmp12, tmp4, tmp13)
tmp15 = triton_helpers.maximum(tmp10, tmp14)
tmp17 = tmp16 == tmp1
tmp19 = tl.where(tmp17, 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_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 4 * (x1 // 4)), xmask)
tmp3 = tl.load(in_ptr1 + x2, xmask)
tmp6 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp1 = 0.0
tmp2 = tmp0 == tmp1
tmp4 = -1.0000000150474662e+30
tmp5 = tl.where(tmp2, tmp4, tmp3)
tmp7 = tmp5 - tmp6
tmp8 = tl_math.exp(tmp7)
tmp10 = tmp8 / tmp9
tl.store(out_ptr0 + x2, tmp10, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (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_1, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_4, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf2)
del primals_2
buf1 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
del buf0
buf3 = reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0)
del buf2
buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(64)](buf1, buf3,
primals_3, buf9, 64, XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf1, reinterpret_tensor(buf3, (4, 4, 4), (16, 1,
4), 0), out=buf4)
buf5 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
buf6 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
triton_poi_fused__softmax_1[grid(16)](primals_5, buf4, buf5, buf6,
16, XBLOCK=16, num_warps=1, num_stages=1)
buf7 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
triton_poi_fused__softmax_2[grid(64)](primals_5, buf4, buf5, buf6,
buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
del buf5
del buf6
buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1),
0), primals_4, out=buf8)
del buf7
return buf8, primals_4, primals_5, reinterpret_tensor(primals_1, (16, 4
), (4, 1), 0), buf1, buf4, buf3, buf9
class SeqAttnMatchNew(nn.Module):
"""
Given sequences X and Y, match sequence Y to each element in X.
* o_i = sum(alpha_j * y_j) for i in X
* alpha_j = softmax(y_j * x_i)
"""
def __init__(self, embed_dim, identity=False):
super(SeqAttnMatchNew, self).__init__()
if not identity:
self.linear = nn.Linear(embed_dim, embed_dim)
else:
self.linear = None
def forward(self, input_0, input_1, input_2):
primals_2 = self.linear.weight
primals_3 = self.linear.bias
primals_1 = input_0
primals_4 = input_1
primals_5 = input_2
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DongjunLee/claf
|
SeqAttnMatch
| false
| 13,600
|
[
"MIT"
] | 225
|
ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
https://github.com/DongjunLee/claf/tree/ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
Classifier
|
import torch
from torch import nn
class Classifier(nn.Module):
def __init__(self, num_inputs1, num_inputs2):
super().__init__()
self.network = nn.Bilinear(num_inputs1, num_inputs2, 1)
def forward(self, x1, x2):
return self.network(x1, x2)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'num_inputs1': 4, 'num_inputs2': 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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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 = args
args.clear()
assert_size_stride(primals_1, (1, 4, 4), (16, 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)
buf0 = torch.ops.aten._trilinear.default(reinterpret_tensor(
primals_4, (64, 4), (4, 1), 0), primals_1, reinterpret_tensor(
primals_3, (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_add_0[grid(64)](buf2, primals_2, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_2
return buf2, reinterpret_tensor(primals_4, (64, 4), (4, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0)
class ClassifierNew(nn.Module):
def __init__(self, num_inputs1, num_inputs2):
super().__init__()
self.network = nn.Bilinear(num_inputs1, num_inputs2, 1)
def forward(self, input_0, input_1):
primals_1 = self.network.weight
primals_2 = self.network.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
DuaneNielsen/atari-representation-learning
|
Classifier
| false
| 13,601
|
[
"MIT"
] | 175
|
fe34f389768416deaa6a6ff0bdebba3d05762a55
|
https://github.com/DuaneNielsen/atari-representation-learning/tree/fe34f389768416deaa6a6ff0bdebba3d05762a55
|
Foo
|
import torch
import torch.nn.functional
import torch.nn.parallel
import torch.utils.data
import torch.optim
import torch.utils.data.distributed
class Foo(torch.nn.Module):
def __init__(self, size):
super(Foo, self).__init__()
self.n = torch.nn.Parameter(torch.ones(size))
self.m = torch.nn.Parameter(torch.ones(size))
def forward(self, input):
return self.n * input + self.m
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'size': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn.functional
import torch.nn.parallel
import torch.utils.data
import torch.optim
import torch.utils.data.distributed
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_mul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 * tmp1
tmp4 = tmp2 + tmp3
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,), (1,))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_mul_0[grid(256)](primals_1, primals_2,
primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
del primals_3
return buf0, primals_2
class FooNew(torch.nn.Module):
def __init__(self, size):
super(FooNew, self).__init__()
self.n = torch.nn.Parameter(torch.ones(size))
self.m = torch.nn.Parameter(torch.ones(size))
def forward(self, input_0):
primals_1 = self.n
primals_3 = self.m
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DonnieKim411/apex
|
Foo
| false
| 13,602
|
[
"BSD-3-Clause"
] | 6,523
|
fb00a5a1d569c7b118aa672b3dacac3663ca3911
|
https://github.com/DonnieKim411/apex/tree/fb00a5a1d569c7b118aa672b3dacac3663ca3911
|
StableBCELoss
|
import torch
import torch.utils.data
class StableBCELoss(torch.nn.modules.Module):
def __init__(self):
super(StableBCELoss, self).__init__()
def forward(self, input, target):
neg_abs = -input.abs()
loss = input.clamp(min=0) - input * target + (1 + neg_abs.exp()).log()
return loss.mean()
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_abs_add_clamp_exp_log_mean_mul_neg_sub_0(in_out_ptr0,
in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp3 = tl.load(in_ptr1 + r0, None)
tmp1 = 0.0
tmp2 = triton_helpers.maximum(tmp0, tmp1)
tmp4 = tmp0 * tmp3
tmp5 = tmp2 - tmp4
tmp6 = tl_math.abs(tmp0)
tmp7 = -tmp6
tmp8 = tl_math.exp(tmp7)
tmp9 = 1.0
tmp10 = tmp8 + tmp9
tmp11 = tl_math.log(tmp10)
tmp12 = tmp5 + tmp11
tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
tmp16 = 256.0
tmp17 = tmp15 / tmp16
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp17, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_abs_add_clamp_exp_log_mean_mul_neg_sub_0[grid(1)](buf1
, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf1,
class StableBCELossNew(torch.nn.modules.Module):
def __init__(self):
super(StableBCELossNew, self).__init__()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ELEKTRONN/elektronn3
|
StableBCELoss
| false
| 13,603
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
DistanceWeightedMSELoss
|
import torch
from torch import nn
import torch.utils.data
class DistanceWeightedMSELoss(nn.Module):
"""Weighted MSE loss for signed euclidean distance transform targets.
By setting ``fg_weight`` to a high value, the errors in foreground
regions are more strongly penalized.
If ``fg_weight=1``, this loss is equivalent to ``torch.nn.MSELoss``.
Requires that targets are transformed with
:py:class:`elektronn3.data.transforms.DistanceTransformTarget`
Per-pixel weights are assigned on the targets as follows:
- each foreground pixel is weighted by ``fg_weight``
- each background pixel is weighted by 1.
"""
def __init__(self, fg_weight=100.0, mask_borders=40):
super().__init__()
self.fg_weight = fg_weight
self.mask_borders = mask_borders
def forward(self, output, target):
mse = nn.functional.mse_loss(output, target, reduction='none')
with torch.no_grad():
weight = torch.ones_like(target)
weight[target <= 0] = self.fg_weight
if self.mask_borders is not None:
o = self.mask_borders
weight[:, :, :o, :o] = 0.0
weight[:, :, target.shape[-2] - o:, target.shape[-1] - o:
] = 0.0
return torch.mean(weight * mse)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch import nn
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_fill_lift_fresh_mean_mse_loss_mul_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 = 0.0
tmp5 = tmp4 * tmp3
tmp6 = tl.broadcast_to(tmp5, [RBLOCK])
tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
tmp9 = 256.0
tmp10 = tmp8 / tmp9
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp10, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf1 = empty_strided_cuda((), (), torch.float32)
buf2 = buf1
del buf1
get_raw_stream(0)
triton_per_fused_fill_lift_fresh_mean_mse_loss_mul_0[grid(1)](buf2,
arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf2,
class DistanceWeightedMSELossNew(nn.Module):
"""Weighted MSE loss for signed euclidean distance transform targets.
By setting ``fg_weight`` to a high value, the errors in foreground
regions are more strongly penalized.
If ``fg_weight=1``, this loss is equivalent to ``torch.nn.MSELoss``.
Requires that targets are transformed with
:py:class:`elektronn3.data.transforms.DistanceTransformTarget`
Per-pixel weights are assigned on the targets as follows:
- each foreground pixel is weighted by ``fg_weight``
- each background pixel is weighted by 1.
"""
def __init__(self, fg_weight=100.0, mask_borders=40):
super().__init__()
self.fg_weight = fg_weight
self.mask_borders = mask_borders
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ELEKTRONN/elektronn3
|
DistanceWeightedMSELoss
| false
| 13,604
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
CoAttention
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class CoAttention(nn.Module):
"""
CoAttention encoder
in Dynamic Coattention Networks For Question Answering (https://arxiv.org/abs/1611.01604)
check the Figure 2 in paper
* Args:
embed_dim: the number of input embedding dimension
"""
def __init__(self, embed_dim):
super(CoAttention, self).__init__()
self.W_0 = nn.Linear(embed_dim * 3, 1, bias=False)
def forward(self, context_embed, question_embed, context_mask=None,
question_mask=None):
C, Q = context_embed, question_embed
B, C_L, Q_L, D = C.size(0), C.size(1), Q.size(1), Q.size(2)
similarity_matrix_shape = torch.zeros(B, C_L, Q_L, D)
C_ = C.unsqueeze(2).expand_as(similarity_matrix_shape)
Q_ = Q.unsqueeze(1).expand_as(similarity_matrix_shape)
C_Q = torch.mul(C_, Q_)
S = self.W_0(torch.cat([C_, Q_, C_Q], 3)).squeeze(3)
S_question = S
if question_mask is not None:
S_question = f.add_masked_value(S_question, question_mask.
unsqueeze(1), value=-10000000.0)
S_q = F.softmax(S_question, 2)
S_context = S.transpose(1, 2)
if context_mask is not None:
S_context = f.add_masked_value(S_context, context_mask.
unsqueeze(1), value=-10000000.0)
S_c = F.softmax(S_context, 2)
A = torch.bmm(S_q, Q)
B = torch.bmm(S_q, S_c).bmm(C)
out = torch.cat([C, A, C * A, C * B], dim=-1)
return out
def get_inputs():
return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'embed_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_cat_0(in_ptr0, in_ptr1, 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 % 12
x4 = xindex // 48
x1 = xindex // 12 % 4
x3 = xindex // 192
x5 = 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 * x4 + 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 + 16 * x3 + (-4 + x0)), tmp9 & xmask,
eviction_policy='evict_last', other=0.0)
tmp11 = tmp0 >= tmp7
tl.full([1], 12, tl.int64)
tmp14 = tl.load(in_ptr0 + (4 * x4 + (-8 + x0)), tmp11 & xmask,
eviction_policy='evict_last', other=0.0)
tmp15 = tl.load(in_ptr1 + (4 * x1 + 16 * x3 + (-8 + x0)), tmp11 & xmask,
eviction_policy='evict_last', other=0.0)
tmp16 = tmp14 * tmp15
tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
tmp18 = tl.where(tmp11, tmp16, tmp17)
tmp19 = tl.where(tmp9, tmp10, tmp18)
tmp20 = tl.where(tmp4, tmp5, tmp19)
tl.store(out_ptr0 + x5, tmp20, xmask)
@triton.jit
def triton_poi_fused__softmax_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
x4 = xindex
x1 = xindex // 4
x0 = xindex % 4
x3 = xindex // 16
tmp0 = tl.load(in_ptr0 + x4, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr0 + (x0 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp11 = tl.load(in_ptr0 + (4 + x0 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp13 = tl.load(in_ptr0 + (8 + x0 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp15 = tl.load(in_ptr0 + (12 + x0 + 16 * x3), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tmp12 = triton_helpers.maximum(tmp10, tmp11)
tmp14 = triton_helpers.maximum(tmp12, tmp13)
tmp16 = triton_helpers.maximum(tmp14, tmp15)
tmp17 = tmp0 - tmp16
tmp18 = tl_math.exp(tmp17)
tl.store(out_ptr0 + x4, tmp9, xmask)
tl.store(out_ptr1 + x4, tmp18, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused__softmax_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')
tmp1 = tl.load(in_ptr0 + (y0 + 16 * y1), ymask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (4 + y0 + 16 * y1), ymask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (8 + y0 + 16 * y1), ymask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (12 + y0 + 16 * y1), ymask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + (x2 + 4 * y3), tmp8, xmask & ymask)
@triton.jit
def triton_poi_fused_cat_4(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16
x2 = xindex
tmp0 = x0
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp6 = 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
tmp12 = tl.full([1], 12, tl.int64)
tmp13 = tmp0 < tmp12
tmp14 = tmp11 & tmp13
tmp15 = tl.load(in_ptr0 + (4 * x1 + (-8 + x0)), tmp14 & xmask,
eviction_policy='evict_last', other=0.0)
tmp16 = tl.load(in_ptr1 + (4 * x1 + (-8 + x0)), tmp14 & xmask,
eviction_policy='evict_last', other=0.0)
tmp17 = tmp15 * tmp16
tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype)
tmp19 = tl.where(tmp14, tmp17, tmp18)
tmp20 = tmp0 >= tmp12
tl.full([1], 16, tl.int64)
tmp23 = tl.load(in_ptr0 + (4 * x1 + (-12 + x0)), tmp20 & xmask,
eviction_policy='evict_last', other=0.0)
tmp24 = tl.load(in_ptr2 + (4 * x1 + (-12 + x0)), tmp20 & xmask,
eviction_policy='evict_last', other=0.0)
tmp25 = tmp23 * tmp24
tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
tmp27 = tl.where(tmp20, tmp25, tmp26)
tmp28 = tl.where(tmp14, tmp19, tmp27)
tmp29 = tl.where(tmp9, tmp10, tmp28)
tmp30 = tl.where(tmp4, tmp5, tmp29)
tl.store(out_ptr0 + x2, tmp30, 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, (1, 12), (12, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 12), (192, 48, 12, 1), torch.
float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(768)](primals_2, primals_1, buf0, 768,
XBLOCK=128, num_warps=4, num_stages=1)
buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (64, 12), (12, 1), 0),
reinterpret_tensor(primals_3, (12, 1), (1, 12), 0), out=buf1)
del primals_3
buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
buf4 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32)
triton_poi_fused__softmax_1[grid(64)](buf1, buf2, buf4, 64, XBLOCK=
64, num_warps=1, num_stages=1)
buf3 = reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0)
del buf1
triton_poi_fused__softmax_2[grid(64)](buf2, buf3, 64, XBLOCK=64,
num_warps=1, num_stages=1)
buf5 = buf2
del buf2
triton_poi_fused__softmax_3[grid(16, 4)](buf4, buf5, 16, 4, XBLOCK=
4, YBLOCK=16, num_warps=1, num_stages=1)
buf6 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
del buf4
extern_kernels.bmm(buf3, primals_1, out=buf6)
buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf3, buf5, out=buf7)
buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(buf7, primals_2, out=buf8)
del buf7
buf9 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
triton_poi_fused_cat_4[grid(256)](primals_2, buf6, buf8, buf9, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del buf6
del buf8
return buf9, primals_2, reinterpret_tensor(buf0, (64, 12), (12, 1), 0
), buf3, buf5, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0)
class CoAttentionNew(nn.Module):
"""
CoAttention encoder
in Dynamic Coattention Networks For Question Answering (https://arxiv.org/abs/1611.01604)
check the Figure 2 in paper
* Args:
embed_dim: the number of input embedding dimension
"""
def __init__(self, embed_dim):
super(CoAttentionNew, self).__init__()
self.W_0 = nn.Linear(embed_dim * 3, 1, bias=False)
def forward(self, input_0, input_1):
primals_3 = self.W_0.weight
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DongjunLee/claf
|
CoAttention
| false
| 13,605
|
[
"MIT"
] | 225
|
ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
https://github.com/DongjunLee/claf/tree/ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
SoftmaxBCELoss
|
import torch
import torch.utils.data
class SoftmaxBCELoss(torch.nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self.bce = torch.nn.BCELoss(*args, **kwargs)
def forward(self, output, target):
probs = torch.nn.functional.softmax(output, dim=1)
return self.bce(probs, target)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x3, tmp9, xmask)
@triton.jit
def triton_per_fused__softmax_binary_cross_entropy_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)
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tmp10 = 1.0
tmp11 = tmp9 - tmp10
tmp12 = -tmp8
tmp13 = libdevice.log1p(tmp12)
tmp14 = -100.0
tmp15 = triton_helpers.maximum(tmp13, tmp14)
tmp16 = tmp11 * tmp15
tmp17 = tl_math.log(tmp8)
tmp18 = triton_helpers.maximum(tmp17, tmp14)
tmp19 = tmp9 * tmp18
tmp20 = tmp16 - tmp19
tmp21 = tl.broadcast_to(tmp20, [RBLOCK])
tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
tmp24 = 256.0
tmp25 = tmp23 / tmp24
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp25, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__softmax_0[grid(256)](arg0_1, buf0, 256, XBLOCK=
256, num_warps=4, num_stages=1)
del arg0_1
buf2 = empty_strided_cuda((), (), torch.float32)
buf3 = buf2
del buf2
triton_per_fused__softmax_binary_cross_entropy_1[grid(1)](buf3,
buf0, arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg1_1
del buf0
return buf3,
class SoftmaxBCELossNew(torch.nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self.bce = torch.nn.BCELoss(*args, **kwargs)
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ELEKTRONN/elektronn3
|
SoftmaxBCELoss
| false
| 13,606
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
LovaszHingeLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class LovaszHingeLoss(nn.Module):
"""
This class implements the lovasz hinge loss which is the continuous of the IoU for binary segmentation.
Source: https://github.com/bermanmaxim/LovaszSoftmax
"""
def __init__(self) ->None:
"""
Constructor method
"""
super(LovaszHingeLoss, self).__init__()
def _calc_grad(self, label_sorted: 'torch.Tensor') ->torch.Tensor:
"""
Method computes the gradients of the sorted and flattened label
:param label_sorted: (torch.Tensor) Sorted and flattened label of shape [n]
:return: (torch.Tensor) Gradient tensor
"""
label_sum = label_sorted.sum()
intersection = label_sum - label_sorted.cumsum(dim=0)
union = label_sum + (1 - label_sorted).cumsum(dim=0)
iou = 1.0 - intersection / union
iou[1:] = iou[1:] - iou[0:-1]
return iou
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the dice loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Dice loss value
"""
prediction = prediction.flatten(start_dim=0)
label = label.flatten(start_dim=0)
signs = 2.0 * label - 1.0
error = 1.0 - prediction * signs
errors_sorted, permutation = torch.sort(error, dim=0, descending=True)
label_sorted = label[permutation]
grad = self._calc_grad(label_sorted)
loss = torch.dot(F.relu(errors_sorted), grad)
return loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def _triton_helper_fn_add0(arg0_0, arg1_0):
tmp0 = arg0_0 + arg1_0
return tmp0
@triton.jit
def triton_per_fused_cumsum_index_mul_rsub_sort_sub_sum_0(in_ptr0, in_ptr1,
out_ptr0, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = 2.0
tmp3 = tmp1 * tmp2
tmp4 = 1.0
tmp5 = tmp3 - tmp4
tmp6 = tmp0 * tmp5
tmp7 = tmp4 - tmp6
tmp8 = r0
tmp9 = tmp8.to(tl.int16)
tmp10 = tl.broadcast_to(tmp7, [RBLOCK])
tmp11 = tl.broadcast_to(tmp9, [RBLOCK])
tmp12, tmp13 = triton_helpers.sort_with_index(tmp10, tmp11, None, 0,
stable=False, descending=True)
tmp14 = tmp13.to(tl.int64)
tmp15 = tl.full([RBLOCK], 256, tl.int32)
tmp16 = tmp14 + tmp15
tmp17 = tmp14 < 0
tmp18 = tl.where(tmp17, tmp16, tmp14)
tl.device_assert((0 <= tmp18) & (tmp18 < 256),
'index out of bounds: 0 <= tmp18 < 256')
tmp20 = tl.load(in_ptr1 + tmp18, None, eviction_policy='evict_last')
tmp21 = tl.broadcast_to(tmp20, [RBLOCK])
tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
tmp24 = tmp20.to(tl.float32)
tmp25 = tl.broadcast_to(tmp24, [RBLOCK])
tmp26, = tl.associative_scan((tmp25,), 0, _triton_helper_fn_add0)
tmp27 = tmp4 - tmp20
tmp28 = tmp27.to(tl.float32)
tmp29 = tl.broadcast_to(tmp28, [RBLOCK])
tmp30, = tl.associative_scan((tmp29,), 0, _triton_helper_fn_add0)
tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp12, None)
tl.store(out_ptr3 + tl.broadcast_to(r0, [RBLOCK]), tmp26, None)
tl.store(out_ptr4 + tl.broadcast_to(r0, [RBLOCK]), tmp30, None)
tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp23, None)
@triton.jit
def triton_per_fused_add_div_dot_relu_rsub_sub_1(in_out_ptr0, in_ptr0,
in_ptr1, in_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp6 = tl.load(in_out_ptr0 + 0)
tmp7 = tl.broadcast_to(tmp6, [RBLOCK])
tmp24 = tl.load(in_ptr1 + r0, None)
tmp26 = tl.load(in_ptr2 + r0, None)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp3 = r0
tmp4 = tl.full([1], 1, tl.int64)
tmp5 = tmp3 >= tmp4
tmp8 = tl.load(in_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp5, other=0.0)
tmp9 = tmp7 - tmp8
tmp10 = tl.load(in_ptr2 + tl.broadcast_to(r0, [RBLOCK]), tmp5, other=0.0)
tmp11 = tmp7 + tmp10
tmp12 = tmp9 / tmp11
tmp13 = 1.0
tmp14 = tmp13 - tmp12
tmp15 = tl.load(in_ptr1 + tl.broadcast_to(-1 + r0, [RBLOCK]), tmp5,
other=0.0)
tmp16 = tmp7 - tmp15
tmp17 = tl.load(in_ptr2 + tl.broadcast_to(-1 + r0, [RBLOCK]), tmp5,
other=0.0)
tmp18 = tmp7 + tmp17
tmp19 = tmp16 / tmp18
tmp20 = tmp13 - tmp19
tmp21 = tmp14 - tmp20
tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
tmp23 = tl.where(tmp5, tmp21, tmp22)
tmp25 = tmp7 - tmp24
tmp27 = tmp7 + tmp26
tmp28 = tmp25 / tmp27
tmp29 = tmp13 - tmp28
tmp30 = tl.where(tmp5, tmp23, tmp29)
tmp31 = tmp2 * tmp30
tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp34, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((256,), (1,), torch.float32)
buf2 = empty_strided_cuda((), (), torch.float32)
buf3 = empty_strided_cuda((256,), (1,), torch.float32)
buf4 = empty_strided_cuda((256,), (1,), torch.float32)
get_raw_stream(0)
triton_per_fused_cumsum_index_mul_rsub_sort_sub_sum_0[grid(1)](arg0_1,
arg1_1, buf0, buf2, buf3, buf4, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
buf5 = buf2
del buf2
triton_per_fused_add_div_dot_relu_rsub_sub_1[grid(1)](buf5, buf0,
buf3, buf4, 1, 256, num_warps=2, num_stages=1)
del buf0
del buf3
del buf4
return buf5,
class LovaszHingeLossNew(nn.Module):
"""
This class implements the lovasz hinge loss which is the continuous of the IoU for binary segmentation.
Source: https://github.com/bermanmaxim/LovaszSoftmax
"""
def __init__(self) ->None:
"""
Constructor method
"""
super(LovaszHingeLossNew, self).__init__()
def _calc_grad(self, label_sorted: 'torch.Tensor') ->torch.Tensor:
"""
Method computes the gradients of the sorted and flattened label
:param label_sorted: (torch.Tensor) Sorted and flattened label of shape [n]
:return: (torch.Tensor) Gradient tensor
"""
label_sum = label_sorted.sum()
intersection = label_sum - label_sorted.cumsum(dim=0)
union = label_sum + (1 - label_sorted).cumsum(dim=0)
iou = 1.0 - intersection / union
iou[1:] = iou[1:] - iou[0:-1]
return iou
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ChristophReich1996/Cell-DETR
|
LovaszHingeLoss
| false
| 13,607
|
[
"MIT"
] | 55
|
4d0c3a2d3ffd19184c8443e5b3a6dccc053c77ea
|
https://github.com/ChristophReich1996/Cell-DETR/tree/4d0c3a2d3ffd19184c8443e5b3a6dccc053c77ea
|
Skip
|
import torch
from torch import nn
class Skip(nn.Module):
def __init__(self, C_in, C_out, stride):
super(Skip, self).__init__()
assert C_out % C_in == 0, 'C_out must be divisible by C_in'
self.repeats = 1, C_out // C_in, 1, 1
def forward(self, x):
return x.repeat(self.repeats)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'C_in': 4, 'C_out': 4, 'stride': 1}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_repeat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tl.store(out_ptr0 + x0, tmp0, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_repeat_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del arg0_1
return buf0,
class SkipNew(nn.Module):
def __init__(self, C_in, C_out, stride):
super(SkipNew, self).__init__()
assert C_out % C_in == 0, 'C_out must be divisible by C_in'
self.repeats = 1, C_out // C_in, 1, 1
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DrSleep/nas-segm-pytorch
|
Skip
| false
| 13,608
|
[
"BSD-2-Clause"
] | 155
|
5de0c5c60cc05f94305ff59ae9f822656e3e7a96
|
https://github.com/DrSleep/nas-segm-pytorch/tree/5de0c5c60cc05f94305ff59ae9f822656e3e7a96
|
CaffeNormalize
|
import torch
import torch.utils.data
import torch.nn as nn
class CaffeNormalize(nn.Module):
def __init__(self, features, eps=1e-07):
super(CaffeNormalize, self).__init__()
self.scale = nn.Parameter(10.0 * torch.ones(features))
self.eps = eps
def forward(self, x):
x_size = x.size()
norm = x.norm(2, dim=1, keepdim=True)
x = x.div(norm + self.eps)
return x.mul(self.scale.view(1, x_size[1], 1, 1))
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.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_div_linalg_vector_norm_mul_0(in_ptr0, in_ptr1,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp16 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp1 * tmp1
tmp4 = tmp3 * tmp3
tmp5 = tmp2 + tmp4
tmp7 = tmp6 * tmp6
tmp8 = tmp5 + tmp7
tmp10 = tmp9 * tmp9
tmp11 = tmp8 + tmp10
tmp12 = libdevice.sqrt(tmp11)
tmp13 = 1e-07
tmp14 = tmp12 + tmp13
tmp15 = tmp0 / tmp14
tmp17 = tmp15 * tmp16
tl.store(out_ptr0 + x3, tmp17, xmask)
def call(args):
primals_1, primals_2 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_linalg_vector_norm_mul_0[grid(256)](primals_1,
primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
return buf0, primals_1
class CaffeNormalizeNew(nn.Module):
def __init__(self, features, eps=1e-07):
super(CaffeNormalizeNew, self).__init__()
self.scale = nn.Parameter(10.0 * torch.ones(features))
self.eps = eps
def forward(self, input_0):
primals_2 = self.scale
primals_1 = input_0
output = call([primals_1, primals_2])
return output[0]
|
E18301194/DepthAwareCNN
|
CaffeNormalize
| false
| 13,609
|
[
"MIT"
] | 278
|
8ae98f7f18b69f79e7df03397dec2543d3d0c8eb
|
https://github.com/E18301194/DepthAwareCNN/tree/8ae98f7f18b69f79e7df03397dec2543d3d0c8eb
|
PolicyNetwork
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class PolicyNetwork(nn.Module):
"""
Deep neural network which represents policy network.
"""
def __init__(self, input_size, num_actions):
super(PolicyNetwork, self).__init__()
self.linear1 = nn.Linear(input_size, 50)
self.linear2 = nn.Linear(50, 50)
self.head = nn.Linear(50, num_actions)
def forward(self, x):
x = F.leaky_relu(self.linear1(x))
x = F.leaky_relu(self.linear2(x))
return F.softmax(self.head(x))
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'num_actions': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 3200
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 50
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = tmp2 > tmp3
tmp5 = 0.01
tmp6 = tmp2 * tmp5
tmp7 = tl.where(tmp4, tmp2, tmp6)
tl.store(out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr1 + x2, tmp7, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x3, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x3, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (50, 4), (4, 1))
assert_size_stride(primals_2, (50,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (50, 50), (50, 1))
assert_size_stride(primals_5, (50,), (1,))
assert_size_stride(primals_6, (4, 50), (50, 1))
assert_size_stride(primals_7, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool)
buf2 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.
float32)
get_raw_stream(0)
triton_poi_fused_leaky_relu_0[grid(3200)](buf0, primals_2, buf1,
buf2, 3200, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf3 = buf0
del buf0
extern_kernels.mm(reinterpret_tensor(buf2, (64, 50), (50, 1), 0),
reinterpret_tensor(primals_4, (50, 50), (1, 50), 0), out=buf3)
buf4 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool)
buf5 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.
float32)
triton_poi_fused_leaky_relu_0[grid(3200)](buf3, primals_5, buf4,
buf5, 3200, XBLOCK=128, num_warps=4, num_stages=1)
del buf3
del primals_5
buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 50),
(50, 1), 0), reinterpret_tensor(primals_6, (50, 4), (1, 50), 0),
alpha=1, beta=1, out=buf6)
del primals_7
buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(256)](buf6, buf7, 256, XBLOCK=256,
num_warps=4, num_stages=1)
buf8 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf6
triton_poi_fused__softmax_2[grid(256)](buf7, buf8, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del buf7
return buf8, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), buf1, reinterpret_tensor(buf2, (64, 50), (50, 1), 0
), buf4, reinterpret_tensor(buf5, (64, 50), (50, 1), 0
), buf8, primals_6, primals_4
class PolicyNetworkNew(nn.Module):
"""
Deep neural network which represents policy network.
"""
def __init__(self, input_size, num_actions):
super(PolicyNetworkNew, self).__init__()
self.linear1 = nn.Linear(input_size, 50)
self.linear2 = nn.Linear(50, 50)
self.head = nn.Linear(50, num_actions)
def forward(self, input_0):
primals_1 = self.linear1.weight
primals_2 = self.linear1.bias
primals_4 = self.linear2.weight
primals_5 = self.linear2.bias
primals_6 = self.head.weight
primals_7 = self.head.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
Dookas/Robust-Multitask-RL
|
PolicyNetwork
| false
| 13,610
|
[
"MIT"
] | 106
|
7970e20cbdf91703c88edcb84568d7354e2525bc
|
https://github.com/Dookas/Robust-Multitask-RL/tree/7970e20cbdf91703c88edcb84568d7354e2525bc
|
LayerNorm
|
import torch
import torch.nn as nn
class LayerNorm(nn.Module):
"""
Layer Normalization
(https://arxiv.org/abs/1607.06450)
"""
def __init__(self, normalized_shape, eps=1e-05):
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(normalized_shape))
self.beta = nn.Parameter(torch.zeros(normalized_shape))
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
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'normalized_shape': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_0(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp8 = tmp6 + tmp7
tmp9 = 4.0
tmp10 = tmp8 / tmp9
tmp11 = tmp1 - tmp10
tmp12 = tmp0 * tmp11
tmp13 = tmp2 - tmp10
tmp14 = tmp13 * tmp13
tmp15 = tmp3 - tmp10
tmp16 = tmp15 * tmp15
tmp17 = tmp14 + tmp16
tmp18 = tmp5 - tmp10
tmp19 = tmp18 * tmp18
tmp20 = tmp17 + tmp19
tmp21 = tmp7 - tmp10
tmp22 = tmp21 * tmp21
tmp23 = tmp20 + tmp22
tmp24 = 3.0
tmp25 = tmp23 / tmp24
tmp26 = libdevice.sqrt(tmp25)
tmp27 = 1e-05
tmp28 = tmp26 + tmp27
tmp29 = tmp12 / tmp28
tmp31 = tmp29 + tmp30
tl.store(out_ptr0 + x2, tmp31, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_mean_mul_std_sub_0[grid(256)](primals_2,
primals_1, primals_3, buf0, 256, XBLOCK=128, num_warps=4,
num_stages=1)
del primals_2
del primals_3
return buf0, primals_1
class LayerNormNew(nn.Module):
"""
Layer Normalization
(https://arxiv.org/abs/1607.06450)
"""
def __init__(self, normalized_shape, eps=1e-05):
super(LayerNormNew, self).__init__()
self.gamma = nn.Parameter(torch.ones(normalized_shape))
self.beta = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
def forward(self, input_0):
primals_2 = self.gamma
primals_3 = self.beta
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
DongjunLee/claf
|
LayerNorm
| false
| 13,611
|
[
"MIT"
] | 225
|
ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
https://github.com/DongjunLee/claf/tree/ef548dda27c9aac8ce4db09774c8a1459d25bde1
|
ResnetBlockConv1D
|
import torch
import torch.nn as nn
class ResnetBlockConv1D(nn.Module):
def __init__(self, size_in, size_out=None, size_h=None):
super().__init__()
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
self.fc_0 = nn.Conv1d(size_in, size_h, 1)
self.fc_1 = nn.Conv1d(size_h, size_out, 1)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False)
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(x))
dx = self.fc_1(self.actvn(net))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {'size_in': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tl.store(out_ptr0 + x0, tmp2, 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
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_add_2(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
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, 1), (4, 1, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_relu_0[grid(16)](primals_1, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (1, 4, 4
), (0, 4, 1), 0), primals_2, stride=(1,), padding=(0,),
dilation=(1,), transposed=False, output_padding=(0,), groups=1,
bias=None)
assert_size_stride(buf1, (1, 4, 4), (16, 4, 1))
buf2 = reinterpret_tensor(buf1, (4, 4), (4, 1), 0)
del buf1
buf5 = empty_strided_cuda((4, 4), (4, 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 = extern_kernels.convolution(reinterpret_tensor(buf2, (1, 4, 4
), (0, 4, 1), 0), primals_4, stride=(1,), padding=(0,),
dilation=(1,), transposed=False, output_padding=(0,), groups=1,
bias=None)
assert_size_stride(buf3, (1, 4, 4), (16, 4, 1))
buf4 = reinterpret_tensor(buf3, (4, 4), (4, 1), 0)
del buf3
triton_poi_fused_add_2[grid(16)](buf4, primals_1, primals_5, 16,
XBLOCK=16, num_warps=1, num_stages=1)
del primals_1
del primals_5
return buf4, primals_2, primals_4, reinterpret_tensor(buf0, (1, 4, 4),
(16, 4, 1), 0), reinterpret_tensor(buf2, (1, 4, 4), (16, 4, 1), 0
), buf5
class ResnetBlockConv1DNew(nn.Module):
def __init__(self, size_in, size_out=None, size_h=None):
super().__init__()
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
self.fc_0 = nn.Conv1d(size_in, size_h, 1)
self.fc_1 = nn.Conv1d(size_h, size_out, 1)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False)
nn.init.zeros_(self.fc_1.weight)
def forward(self, input_0):
primals_2 = self.fc_0.weight
primals_3 = self.fc_0.bias
primals_4 = self.fc_1.weight
primals_5 = self.fc_1.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DveloperY0115/texture_fields
|
ResnetBlockConv1D
| false
| 13,612
|
[
"MIT"
] | 78
|
28c277696e0a658ffff3496892810d5a0ef03f65
|
https://github.com/DveloperY0115/texture_fields/tree/28c277696e0a658ffff3496892810d5a0ef03f65
|
Net
|
import torch
import torch.nn as nn
import torch.nn.functional as F
def set_init(layers):
for layer in layers:
nn.init.normal(layer.weight, mean=0.0, std=0.1)
nn.init.constant(layer.bias, 0.1)
class Net(nn.Module):
def __init__(self, s_dim, a_dim):
super(Net, self).__init__()
self.s_dim = s_dim
self.a_dim = a_dim
self.pi1 = nn.Linear(s_dim, 50)
self.pi2 = nn.Linear(50, 50)
self.pi3 = nn.Linear(50, a_dim)
self.v1 = nn.Linear(s_dim, 50)
self.v2 = nn.Linear(50, 50)
self.v3 = nn.Linear(50, 1)
set_init([self.pi1, self.pi2, self.pi3, self.v1, self.v2, self.v3])
self.distribution = torch.distributions.Categorical
def forward(self, x):
pi1 = F.relu(self.pi1(x))
logits = self.pi3(F.relu(self.pi2(pi1)))
v1 = F.relu(self.v1(x))
values = self.v3(F.relu(self.v2(v1)))
return logits, values
def choose_action(self, s):
self.eval()
logits, _ = self.forward(s)
prob = F.softmax(logits, dim=1).data
m = self.distribution(prob)
return m.sample().numpy()[0]
def loss_func(self, s, a, v_t):
self.train()
logits, values = self.forward(s)
td = v_t - values
c_loss = td.pow(2)
probs = F.softmax(logits, dim=1)
m = self.distribution(probs)
exp_v = m.log_prob(a) * td.detach()
a_loss = -exp_v
total_loss = (c_loss + a_loss).mean()
return total_loss
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'s_dim': 4, 'a_dim': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 3200
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 50
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13) = args
args.clear()
assert_size_stride(primals_1, (50, 4), (4, 1))
assert_size_stride(primals_2, (50,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (50, 50), (50, 1))
assert_size_stride(primals_5, (50,), (1,))
assert_size_stride(primals_6, (4, 50), (50, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (50, 4), (4, 1))
assert_size_stride(primals_9, (50,), (1,))
assert_size_stride(primals_10, (50, 50), (50, 1))
assert_size_stride(primals_11, (50,), (1,))
assert_size_stride(primals_12, (1, 50), (50, 1))
assert_size_stride(primals_13, (1,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 50), (800, 200, 50, 1), 0)
del buf0
buf14 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool
)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf1,
primals_2, buf14, 3200, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf1, (64, 50), (50, 1), 0),
reinterpret_tensor(primals_4, (50, 50), (1, 50), 0), out=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 50), (800, 200, 50, 1), 0)
del buf2
buf13 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool
)
triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf3,
primals_5, buf13, 3200, XBLOCK=256, num_warps=4, num_stages=1)
del primals_5
buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 50),
(50, 1), 0), reinterpret_tensor(primals_6, (50, 4), (1, 50), 0),
alpha=1, beta=1, out=buf4)
del primals_7
buf5 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_8, (4, 50), (1, 4), 0), out=buf5)
del primals_8
buf6 = reinterpret_tensor(buf5, (4, 4, 4, 50), (800, 200, 50, 1), 0)
del buf5
buf12 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool
)
triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf6,
primals_9, buf12, 3200, XBLOCK=256, num_warps=4, num_stages=1)
del primals_9
buf7 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf6, (64, 50), (50, 1), 0),
reinterpret_tensor(primals_10, (50, 50), (1, 50), 0), out=buf7)
buf8 = reinterpret_tensor(buf7, (4, 4, 4, 50), (800, 200, 50, 1), 0)
del buf7
buf11 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool
)
triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf8,
primals_11, buf11, 3200, XBLOCK=256, num_warps=4, num_stages=1)
del primals_11
buf10 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
extern_kernels.addmm(primals_13, reinterpret_tensor(buf8, (64, 50),
(50, 1), 0), reinterpret_tensor(primals_12, (50, 1), (1, 50), 0
), alpha=1, beta=1, out=buf10)
del primals_13
return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0
), reinterpret_tensor(buf10, (4, 4, 4, 1), (16, 4, 1, 1), 0
), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 50), (50, 1), 0), reinterpret_tensor(
buf3, (64, 50), (50, 1), 0), reinterpret_tensor(buf6, (64, 50), (50,
1), 0), reinterpret_tensor(buf8, (64, 50), (50, 1), 0
), primals_12, buf11, primals_10, buf12, primals_6, buf13, primals_4, buf14
def set_init(layers):
for layer in layers:
nn.init.normal(layer.weight, mean=0.0, std=0.1)
nn.init.constant(layer.bias, 0.1)
class NetNew(nn.Module):
def __init__(self, s_dim, a_dim):
super(NetNew, self).__init__()
self.s_dim = s_dim
self.a_dim = a_dim
self.pi1 = nn.Linear(s_dim, 50)
self.pi2 = nn.Linear(50, 50)
self.pi3 = nn.Linear(50, a_dim)
self.v1 = nn.Linear(s_dim, 50)
self.v2 = nn.Linear(50, 50)
self.v3 = nn.Linear(50, 1)
set_init([self.pi1, self.pi2, self.pi3, self.v1, self.v2, self.v3])
self.distribution = torch.distributions.Categorical
def choose_action(self, s):
self.eval()
logits, _ = self.forward(s)
prob = F.softmax(logits, dim=1).data
m = self.distribution(prob)
return m.sample().numpy()[0]
def loss_func(self, s, a, v_t):
self.train()
logits, values = self.forward(s)
td = v_t - values
c_loss = td.pow(2)
probs = F.softmax(logits, dim=1)
m = self.distribution(probs)
exp_v = m.log_prob(a) * td.detach()
a_loss = -exp_v
total_loss = (c_loss + a_loss).mean()
return total_loss
def forward(self, input_0):
primals_1 = self.pi1.weight
primals_2 = self.pi1.bias
primals_4 = self.pi2.weight
primals_5 = self.pi2.bias
primals_6 = self.pi3.weight
primals_7 = self.pi3.bias
primals_8 = self.v1.weight
primals_9 = self.v1.bias
primals_10 = self.v2.weight
primals_11 = self.v2.bias
primals_12 = self.v3.weight
primals_13 = self.v3.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13])
return output[0], output[1]
|
Dookas/Robust-Multitask-RL
|
Net
| false
| 13,613
|
[
"MIT"
] | 106
|
7970e20cbdf91703c88edcb84568d7354e2525bc
|
https://github.com/Dookas/Robust-Multitask-RL/tree/7970e20cbdf91703c88edcb84568d7354e2525bc
|
SimpleCNN
|
import torch
import torch.nn as nn
from collections import OrderedDict
class SimpleCNN(nn.Module):
def __init__(self, input_dim=3, global_pool=False):
super(SimpleCNN, self).__init__()
self.features = nn.Sequential(OrderedDict([('conv1', nn.Conv2d(
input_dim, 64, kernel_size=3, stride=1, padding=1)), ('bn1', nn
.GroupNorm(32, 64)), ('relu1', nn.ReLU(inplace=True)), ('pool1',
nn.MaxPool2d(kernel_size=2, stride=2)), ('conv2', nn.Conv2d(64,
96, kernel_size=3, stride=1, padding=1)), ('bn2', nn.GroupNorm(
32, 96)), ('relu2', nn.ReLU(inplace=True)), ('pool2', nn.
MaxPool2d(kernel_size=2, stride=2)), ('conv3', nn.Conv2d(96,
128, kernel_size=3, stride=1, padding=1)), ('bn3', nn.GroupNorm
(32, 128)), ('relu3', nn.ReLU(inplace=True)), ('pool3', nn.
MaxPool2d(kernel_size=2, stride=2)), ('conv4', nn.Conv2d(128,
256, kernel_size=3, stride=1, padding=1)), ('bn4', nn.GroupNorm
(32, 256)), ('relu4', nn.ReLU(inplace=True))]))
self.final_dim = 256
self.global_pool = global_pool
def forward(self, x):
feature = self.features(x)
if self.global_pool:
feature = torch.mean(feature, dim=2)
feature = torch.mean(feature, dim=2)
return feature
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
import torch.nn as nn
from collections import OrderedDict
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 192
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 3
y1 = yindex // 3
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask & ymask, eviction_policy=
'evict_last')
tl.store(out_ptr0 + (y0 + 3 * x2 + 27 * y1), tmp0, xmask & ymask)
@triton.jit
def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 12
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
x2 = xindex
y3 = yindex
y0 = yindex % 3
y1 = yindex // 3
tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy=
'evict_last')
tl.store(out_ptr0 + (y0 + 3 * x2 + 12288 * y1), tmp0, ymask)
@triton.jit
def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 64
y1 = yindex // 64
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 96
y1 = yindex // 96
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 96 * x2 + 864 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
xnumel = 9
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y3 = yindex
y0 = yindex % 128
y1 = yindex // 128
tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last'
)
tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask)
@triton.jit
def triton_poi_fused_convolution_5(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 64
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, None)
@triton.jit
def triton_red_fused_native_group_norm_6(in_ptr0, out_ptr0, out_ptr1,
out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
xnumel = 128
rnumel = 8192
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rbase = tl.arange(0, RBLOCK)[None, :]
x0 = xindex % 32
x1 = xindex // 32
tmp2_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp2_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp2_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
x4 = xindex
for roffset in range(0, rnumel, RBLOCK):
rindex = roffset + rbase
rmask = rindex < rnumel
r2 = rindex % 2
r3 = rindex // 2
tmp0 = tl.load(in_ptr0 + (r2 + 2 * x0 + 64 * r3 + 262144 * x1),
rmask & xmask, eviction_policy='evict_first', other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp2_mean_next, tmp2_m2_next, tmp2_weight_next = (triton_helpers.
welford_reduce(tmp1, tmp2_mean, tmp2_m2, tmp2_weight, roffset == 0)
)
tmp2_mean = tl.where(rmask & xmask, tmp2_mean_next, tmp2_mean)
tmp2_m2 = tl.where(rmask & xmask, tmp2_m2_next, tmp2_m2)
tmp2_weight = tl.where(rmask & xmask, tmp2_weight_next, tmp2_weight)
tmp2_tmp, tmp3_tmp, tmp4_tmp = triton_helpers.welford(tmp2_mean,
tmp2_m2, tmp2_weight, 1)
tmp2 = tmp2_tmp[:, None]
tmp3 = tmp3_tmp[:, None]
tmp4_tmp[:, None]
tl.store(out_ptr0 + x4, tmp2, xmask)
tl.store(out_ptr1 + x4, tmp3, xmask)
tmp5 = 8192.0
tmp6 = tmp3 / tmp5
tmp7 = 1e-05
tmp8 = tmp6 + tmp7
tmp9 = libdevice.rsqrt(tmp8)
tl.store(out_ptr2 + x4, tmp9, xmask)
@triton.jit
def triton_poi_fused_native_group_norm_relu_7(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
x0 = xindex % 64
x2 = xindex // 262144
tmp0 = tl.load(in_ptr0 + x3, None)
tmp1 = tl.load(in_ptr1 + (32 * x2 + x0 // 2), None, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr2 + (32 * x2 + x0 // 2), None, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr3 + x0, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr4 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = 8192.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_max_pool2d_with_indices_8(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 64
x1 = xindex // 64 % 32
x2 = xindex // 2048
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 128 * x1 + 8192 * x2), None)
tmp1 = tl.load(in_ptr0 + (64 + x0 + 128 * x1 + 8192 * x2), None)
tmp3 = tl.load(in_ptr0 + (4096 + x0 + 128 * x1 + 8192 * x2), None)
tmp5 = tl.load(in_ptr0 + (4160 + x0 + 128 * x1 + 8192 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_9(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 96
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, None)
@triton.jit
def triton_red_fused_native_group_norm_10(in_ptr0, out_ptr0, out_ptr1,
out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
xnumel = 128
rnumel = 3072
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rbase = tl.arange(0, RBLOCK)[None, :]
x0 = xindex % 32
x1 = xindex // 32
tmp2_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp2_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
tmp2_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
x4 = xindex
for roffset in range(0, rnumel, RBLOCK):
rindex = roffset + rbase
rmask = rindex < rnumel
r2 = rindex % 3
r3 = rindex // 3
tmp0 = tl.load(in_ptr0 + (r2 + 3 * x0 + 96 * r3 + 98304 * x1),
rmask & xmask, eviction_policy='evict_first', other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp2_mean_next, tmp2_m2_next, tmp2_weight_next = (triton_helpers.
welford_reduce(tmp1, tmp2_mean, tmp2_m2, tmp2_weight, roffset == 0)
)
tmp2_mean = tl.where(rmask & xmask, tmp2_mean_next, tmp2_mean)
tmp2_m2 = tl.where(rmask & xmask, tmp2_m2_next, tmp2_m2)
tmp2_weight = tl.where(rmask & xmask, tmp2_weight_next, tmp2_weight)
tmp2_tmp, tmp3_tmp, tmp4_tmp = triton_helpers.welford(tmp2_mean,
tmp2_m2, tmp2_weight, 1)
tmp2 = tmp2_tmp[:, None]
tmp3 = tmp3_tmp[:, None]
tmp4_tmp[:, None]
tl.store(out_ptr0 + x4, tmp2, xmask)
tl.store(out_ptr1 + x4, tmp3, xmask)
tmp5 = 3072.0
tmp6 = tmp3 / tmp5
tmp7 = 1e-05
tmp8 = tmp6 + tmp7
tmp9 = libdevice.rsqrt(tmp8)
tl.store(out_ptr2 + x4, tmp9, xmask)
@triton.jit
def triton_poi_fused_native_group_norm_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)
x3 = xindex
x0 = xindex % 96
x2 = xindex // 98304
tmp0 = tl.load(in_ptr0 + x3, None)
tmp1 = tl.load(in_ptr1 + (32 * x2 + x0 // 3), None, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr2 + (32 * x2 + x0 // 3), None, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr3 + x0, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr4 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = 3072.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_max_pool2d_with_indices_12(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 96
x1 = xindex // 96 % 16
x2 = xindex // 1536
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 192 * x1 + 6144 * x2), None)
tmp1 = tl.load(in_ptr0 + (96 + x0 + 192 * x1 + 6144 * x2), None)
tmp3 = tl.load(in_ptr0 + (3072 + x0 + 192 * x1 + 6144 * x2), None)
tmp5 = tl.load(in_ptr0 + (3168 + x0 + 192 * x1 + 6144 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_13(in_out_ptr0, in_ptr0, xnumel, XBLOCK:
tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, None)
@triton.jit
def triton_per_fused_native_group_norm_14(in_ptr0, out_ptr0, out_ptr1,
out_ptr2, 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 % 4
r3 = rindex // 4
x0 = xindex % 32
x1 = xindex // 32
x4 = xindex
tmp0 = tl.load(in_ptr0 + (r2 + 4 * x0 + 128 * r3 + 32768 * x1), None)
tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
tmp3 = tl.broadcast_to(tmp1, [RBLOCK])
tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
tmp6 = tl.full([1], 1024, tl.int32)
tmp7 = tmp6.to(tl.float32)
tmp8 = tmp5 / tmp7
tmp9 = tmp1 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
tmp14 = 1024.0
tmp15 = tmp13 / tmp14
tmp16 = 1e-05
tmp17 = tmp15 + tmp16
tmp18 = libdevice.rsqrt(tmp17)
tl.store(out_ptr2 + x4, tmp18, None)
tl.store(out_ptr0 + x4, tmp8, None)
tl.store(out_ptr1 + x4, tmp13, None)
@triton.jit
def triton_poi_fused_native_group_norm_relu_15(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
x0 = xindex % 128
x2 = xindex // 32768
tmp0 = tl.load(in_ptr0 + x3, None)
tmp1 = tl.load(in_ptr1 + (32 * x2 + x0 // 4), None, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr2 + (32 * x2 + x0 // 4), None, eviction_policy=
'evict_last')
tmp10 = tl.load(in_ptr3 + x0, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr4 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = 1024.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_max_pool2d_with_indices_16(in_ptr0, out_ptr0, out_ptr1,
xnumel, XBLOCK: tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x0 = xindex % 128
x1 = xindex // 128 % 8
x2 = xindex // 1024
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 256 * x1 + 4096 * x2), None)
tmp1 = tl.load(in_ptr0 + (128 + x0 + 256 * x1 + 4096 * x2), None)
tmp3 = tl.load(in_ptr0 + (2048 + x0 + 256 * x1 + 4096 * x2), None)
tmp5 = tl.load(in_ptr0 + (2176 + x0 + 256 * x1 + 4096 * x2), None)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp6 = triton_helpers.maximum(tmp5, tmp4)
tmp7 = tmp1 > tmp0
tmp8 = tl.full([1], 1, tl.int8)
tmp9 = tl.full([1], 0, tl.int8)
tmp10 = tl.where(tmp7, tmp8, tmp9)
tmp11 = tmp3 > tmp2
tmp12 = tl.full([1], 2, tl.int8)
tmp13 = tl.where(tmp11, tmp12, tmp10)
tmp14 = tmp5 > tmp4
tmp15 = tl.full([1], 3, tl.int8)
tmp16 = tl.where(tmp14, tmp15, tmp13)
tl.store(out_ptr0 + x3, tmp6, None)
tl.store(out_ptr1 + x3, tmp16, None)
@triton.jit
def triton_poi_fused_convolution_17(in_out_ptr0, in_ptr0, xnumel, XBLOCK:
tl.constexpr):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 256
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, None)
@triton.jit
def triton_per_fused_native_group_norm_18(in_ptr0, out_ptr0, out_ptr1,
out_ptr2, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 512
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 % 8
r3 = rindex // 8
x0 = xindex % 32
x1 = xindex // 32
x4 = xindex
tmp0 = tl.load(in_ptr0 + (r2 + 8 * x0 + 256 * r3 + 16384 * x1), None)
tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
tmp3 = tl.broadcast_to(tmp1, [RBLOCK])
tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
tmp6 = tl.full([1], 512, tl.int32)
tmp7 = tmp6.to(tl.float32)
tmp8 = tmp5 / tmp7
tmp9 = tmp1 - tmp8
tmp10 = tmp9 * tmp9
tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
tmp14 = 512.0
tmp15 = tmp13 / tmp14
tmp16 = 1e-05
tmp17 = tmp15 + tmp16
tmp18 = libdevice.rsqrt(tmp17)
tl.store(out_ptr2 + x4, tmp18, None)
tl.store(out_ptr0 + x4, tmp8, None)
tl.store(out_ptr1 + x4, tmp13, None)
@triton.jit
def triton_poi_fused_native_group_norm_relu_threshold_backward_19(in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, ynumel, xnumel,
YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
xnumel = 64
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
tl.full([XBLOCK, YBLOCK], True, tl.int1)
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x2 = xindex
y0 = yindex % 256
y1 = yindex // 256
y3 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 16384 * y1), xmask,
eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + y3 // 8, None, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr2 + y3 // 8, None, eviction_policy='evict_last')
tmp10 = tl.load(in_ptr3 + y0, None, eviction_policy='evict_last')
tmp12 = tl.load(in_ptr4 + y0, None, eviction_policy='evict_last')
tmp2 = tmp0 - tmp1
tmp4 = 512.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, 1], 0, tl.int32)
tmp15 = triton_helpers.maximum(tmp14, tmp13)
tmp16 = 0.0
tmp17 = tmp15 <= tmp16
tl.store(out_ptr0 + (x2 + 64 * y3), tmp15, xmask)
tl.store(out_ptr1 + (y0 + 256 * x2 + 16384 * y1), tmp17, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
primals_13, primals_14, primals_15, primals_16, primals_17) = args
args.clear()
assert_size_stride(primals_1, (64, 3, 3, 3), (27, 9, 3, 1))
assert_size_stride(primals_2, (64,), (1,))
assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1))
assert_size_stride(primals_4, (64,), (1,))
assert_size_stride(primals_5, (64,), (1,))
assert_size_stride(primals_6, (96, 64, 3, 3), (576, 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, (128, 96, 3, 3), (864, 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, (256, 128, 3, 3), (1152, 9, 3, 1))
assert_size_stride(primals_15, (256,), (1,))
assert_size_stride(primals_16, (256,), (1,))
assert_size_stride(primals_17, (256,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 3, 3, 3), (27, 1, 9, 3), torch.float32)
get_raw_stream(0)
triton_poi_fused_0[grid(192, 9)](primals_1, buf0, 192, 9, XBLOCK=16,
YBLOCK=64, num_warps=4, num_stages=1)
del primals_1
buf1 = empty_strided_cuda((4, 3, 64, 64), (12288, 1, 192, 3), torch
.float32)
triton_poi_fused_1[grid(12, 4096)](primals_3, buf1, 12, 4096,
XBLOCK=64, YBLOCK=16, num_warps=4, num_stages=1)
del primals_3
buf2 = empty_strided_cuda((96, 64, 3, 3), (576, 1, 192, 64), torch.
float32)
triton_poi_fused_2[grid(6144, 9)](primals_6, buf2, 6144, 9, XBLOCK=
16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_6
buf3 = empty_strided_cuda((128, 96, 3, 3), (864, 1, 288, 96), torch
.float32)
triton_poi_fused_3[grid(12288, 9)](primals_10, buf3, 12288, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_10
buf4 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128),
torch.float32)
triton_poi_fused_4[grid(32768, 9)](primals_14, buf4, 32768, 9,
XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
del primals_14
buf5 = extern_kernels.convolution(buf1, buf0, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf5, (4, 64, 64, 64), (262144, 1, 4096, 64))
buf6 = buf5
del buf5
triton_poi_fused_convolution_5[grid(1048576)](buf6, primals_2,
1048576, XBLOCK=512, num_warps=8, num_stages=1)
del primals_2
buf7 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
buf8 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
buf10 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
triton_red_fused_native_group_norm_6[grid(128)](buf6, buf7, buf8,
buf10, 128, 8192, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1
)
buf11 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64),
torch.float32)
triton_poi_fused_native_group_norm_relu_7[grid(1048576)](buf6, buf7,
buf8, primals_4, primals_5, buf11, 1048576, XBLOCK=1024,
num_warps=4, num_stages=1)
del primals_5
buf12 = empty_strided_cuda((4, 64, 32, 32), (65536, 1, 2048, 64),
torch.float32)
buf13 = empty_strided_cuda((4, 64, 32, 32), (65536, 1, 2048, 64),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_8[grid(262144)](buf11,
buf12, buf13, 262144, XBLOCK=512, num_warps=8, num_stages=1)
buf14 = extern_kernels.convolution(buf12, buf2, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf14, (4, 96, 32, 32), (98304, 1, 3072, 96))
buf15 = buf14
del buf14
triton_poi_fused_convolution_9[grid(393216)](buf15, primals_7,
393216, XBLOCK=512, num_warps=8, num_stages=1)
del primals_7
buf16 = buf8
del buf8
buf17 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
buf19 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
triton_red_fused_native_group_norm_10[grid(128)](buf15, buf16,
buf17, buf19, 128, 3072, XBLOCK=1, RBLOCK=2048, num_warps=16,
num_stages=1)
buf20 = empty_strided_cuda((4, 96, 32, 32), (98304, 1, 3072, 96),
torch.float32)
triton_poi_fused_native_group_norm_relu_11[grid(393216)](buf15,
buf16, buf17, primals_8, primals_9, buf20, 393216, XBLOCK=512,
num_warps=8, num_stages=1)
del primals_9
buf21 = empty_strided_cuda((4, 96, 16, 16), (24576, 1, 1536, 96),
torch.float32)
buf22 = empty_strided_cuda((4, 96, 16, 16), (24576, 1, 1536, 96),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_12[grid(98304)](buf20,
buf21, buf22, 98304, XBLOCK=512, num_warps=8, num_stages=1)
buf23 = extern_kernels.convolution(buf21, buf3, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf23, (4, 128, 16, 16), (32768, 1, 2048, 128))
buf24 = buf23
del buf23
triton_poi_fused_convolution_13[grid(131072)](buf24, primals_11,
131072, XBLOCK=512, num_warps=8, num_stages=1)
del primals_11
buf25 = buf17
del buf17
buf26 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
buf28 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
triton_per_fused_native_group_norm_14[grid(128)](buf24, buf25,
buf26, buf28, 128, 1024, num_warps=8, num_stages=1)
buf29 = empty_strided_cuda((4, 128, 16, 16), (32768, 1, 2048, 128),
torch.float32)
triton_poi_fused_native_group_norm_relu_15[grid(131072)](buf24,
buf25, buf26, primals_12, primals_13, buf29, 131072, XBLOCK=512,
num_warps=8, num_stages=1)
del primals_13
buf30 = empty_strided_cuda((4, 128, 8, 8), (8192, 1, 1024, 128),
torch.float32)
buf31 = empty_strided_cuda((4, 128, 8, 8), (8192, 1, 1024, 128),
torch.int8)
triton_poi_fused_max_pool2d_with_indices_16[grid(32768)](buf29,
buf30, buf31, 32768, XBLOCK=128, num_warps=4, num_stages=1)
buf32 = extern_kernels.convolution(buf30, buf4, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf32, (4, 256, 8, 8), (16384, 1, 2048, 256))
buf33 = buf32
del buf32
triton_poi_fused_convolution_17[grid(65536)](buf33, primals_15,
65536, XBLOCK=512, num_warps=4, num_stages=1)
del primals_15
buf34 = buf26
del buf26
buf35 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
buf37 = empty_strided_cuda((4, 32, 1, 1), (32, 1, 128, 128), torch.
float32)
triton_per_fused_native_group_norm_18[grid(128)](buf33, buf34,
buf35, buf37, 128, 512, num_warps=4, num_stages=1)
buf38 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch
.float32)
buf39 = empty_strided_cuda((4, 256, 8, 8), (16384, 1, 2048, 256),
torch.bool)
triton_poi_fused_native_group_norm_relu_threshold_backward_19[grid(
1024, 64)](buf33, buf34, buf35, primals_16, primals_17, buf38,
buf39, 1024, 64, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
del buf35
del primals_17
return (buf38, buf0, buf1, primals_4, buf2, primals_8, buf3, primals_12,
buf4, primals_16, buf6, reinterpret_tensor(buf7, (4, 32), (32, 1),
0), reinterpret_tensor(buf10, (4, 32), (32, 1), 0), buf11, buf12,
buf13, buf15, reinterpret_tensor(buf16, (4, 32), (32, 1), 0),
reinterpret_tensor(buf19, (4, 32), (32, 1), 0), buf20, buf21, buf22,
buf24, reinterpret_tensor(buf25, (4, 32), (32, 1), 0),
reinterpret_tensor(buf28, (4, 32), (32, 1), 0), buf29, buf30, buf31,
buf33, reinterpret_tensor(buf34, (4, 32), (32, 1), 0),
reinterpret_tensor(buf37, (4, 32), (32, 1), 0), buf39)
class SimpleCNNNew(nn.Module):
def __init__(self, input_dim=3, global_pool=False):
super(SimpleCNNNew, self).__init__()
self.features = nn.Sequential(OrderedDict([('conv1', nn.Conv2d(
input_dim, 64, kernel_size=3, stride=1, padding=1)), ('bn1', nn
.GroupNorm(32, 64)), ('relu1', nn.ReLU(inplace=True)), ('pool1',
nn.MaxPool2d(kernel_size=2, stride=2)), ('conv2', nn.Conv2d(64,
96, kernel_size=3, stride=1, padding=1)), ('bn2', nn.GroupNorm(
32, 96)), ('relu2', nn.ReLU(inplace=True)), ('pool2', nn.
MaxPool2d(kernel_size=2, stride=2)), ('conv3', nn.Conv2d(96,
128, kernel_size=3, stride=1, padding=1)), ('bn3', nn.GroupNorm
(32, 128)), ('relu3', nn.ReLU(inplace=True)), ('pool3', nn.
MaxPool2d(kernel_size=2, stride=2)), ('conv4', nn.Conv2d(128,
256, kernel_size=3, stride=1, padding=1)), ('bn4', nn.GroupNorm
(32, 256)), ('relu4', nn.ReLU(inplace=True))]))
self.final_dim = 256
self.global_pool = global_pool
def forward(self, input_0):
primals_1 = self.features.conv1.weight
primals_2 = self.features.conv1.bias
primals_4 = self.features.bn1.weight
primals_5 = self.features.bn1.bias
primals_6 = self.features.conv2.weight
primals_7 = self.features.conv2.bias
primals_8 = self.features.bn2.weight
primals_9 = self.features.bn2.bias
primals_10 = self.features.conv3.weight
primals_11 = self.features.conv3.bias
primals_12 = self.features.bn3.weight
primals_13 = self.features.bn3.bias
primals_14 = self.features.conv4.weight
primals_15 = self.features.conv4.bias
primals_16 = self.features.bn4.weight
primals_17 = self.features.bn4.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7, primals_8, primals_9,
primals_10, primals_11, primals_12, primals_13, primals_14,
primals_15, primals_16, primals_17])
return output[0]
|
D-X-Y/MSPLD-2018
|
SimpleCNN
| false
| 13,614
|
[
"MIT"
] | 63
|
71a6a75830ac84c7a861e63367ad3ace991fae77
|
https://github.com/D-X-Y/MSPLD-2018/tree/71a6a75830ac84c7a861e63367ad3ace991fae77
|
Policy
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class Policy(nn.Module):
def __init__(self, input_size, num_actions):
super(Policy, self).__init__()
self.affines = nn.Linear(input_size, 100)
self.action_head = nn.Linear(100, num_actions)
self.saved_actions = []
self.rewards = []
def forward(self, x):
action_scores = F.softmax(self.action_head(F.relu(self.affines(x))),
dim=-1)
return action_scores
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'input_size': 4, 'num_actions': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 6400
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x4 = xindex
x0 = xindex % 100
x2 = xindex % 1600
x3 = xindex // 1600
tmp0 = tl.load(in_out_ptr0 + x4, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x4, tmp4, xmask)
tl.store(out_ptr0 + (x2 + 1664 * x3), tmp6, xmask)
@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp5 = triton_helpers.maximum(tmp3, tmp4)
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = tmp0 - tmp7
tmp9 = tl_math.exp(tmp8)
tl.store(out_ptr0 + x2, tmp9, xmask)
@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (100, 4), (4, 1))
assert_size_stride(primals_2, (100,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 100), (100, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 100), (100, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 100), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 100), (1600, 400, 100, 1), 0)
del buf0
buf5 = empty_strided_cuda((4, 4, 4, 100), (1664, 400, 100, 1),
torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(6400)](buf1,
primals_2, buf5, 6400, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 100),
(100, 1), 0), reinterpret_tensor(primals_4, (100, 4), (1, 100),
0), alpha=1, beta=1, out=buf2)
del primals_5
buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(256)](buf2, buf3, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf4 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf2
triton_poi_fused__softmax_2[grid(256)](buf3, buf4, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del buf3
return buf4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 100), (100, 1), 0
), buf4, primals_4, buf5
class PolicyNew(nn.Module):
def __init__(self, input_size, num_actions):
super(PolicyNew, self).__init__()
self.affines = nn.Linear(input_size, 100)
self.action_head = nn.Linear(100, num_actions)
self.saved_actions = []
self.rewards = []
def forward(self, input_0):
primals_1 = self.affines.weight
primals_2 = self.affines.bias
primals_4 = self.action_head.weight
primals_5 = self.action_head.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
Dookas/Robust-Multitask-RL
|
Policy
| false
| 13,615
|
[
"MIT"
] | 106
|
7970e20cbdf91703c88edcb84568d7354e2525bc
|
https://github.com/Dookas/Robust-Multitask-RL/tree/7970e20cbdf91703c88edcb84568d7354e2525bc
|
OnnxErf
|
import torch
from torch import nn
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxErf(nn.Module, OnnxToTorchModule):
def forward(self, input_tensor: 'torch.Tensor') ->torch.Tensor:
return torch.erf(input_tensor)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_erf_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = libdevice.erf(tmp0)
tl.store(out_ptr0 + x0, tmp1, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_erf_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256,
num_warps=4, num_stages=1)
del arg0_1
return buf0,
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxErfNew(nn.Module, OnnxToTorchModule):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxErf
| false
| 13,616
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
OnnxHardSigmoid
|
import torch
from torch import nn
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxHardSigmoid(nn.Module, OnnxToTorchModule):
def __init__(self, alpha: 'float'=0.2, beta: 'float'=0.5):
super().__init__()
self.alpha = alpha
self.beta = beta
def forward(self, input_tensor: 'torch.Tensor') ->torch.Tensor:
return torch.clip(self.alpha * input_tensor + self.beta, min=0.0,
max=1.0)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_clamp_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.2
tmp2 = tmp0 * tmp1
tmp3 = 0.5
tmp4 = tmp2 + tmp3
tmp5 = 0.0
tmp6 = triton_helpers.maximum(tmp4, tmp5)
tmp7 = 1.0
tmp8 = triton_helpers.minimum(tmp6, tmp7)
tl.store(out_ptr0 + x0, tmp8, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_clamp_mul_0[grid(256)](arg0_1, buf0, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxHardSigmoidNew(nn.Module, OnnxToTorchModule):
def __init__(self, alpha: 'float'=0.2, beta: 'float'=0.5):
super().__init__()
self.alpha = alpha
self.beta = beta
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxHardSigmoid
| false
| 13,617
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
ResnetBlockFC
|
import torch
import torch.nn as nn
class ResnetBlockFC(nn.Module):
def __init__(self, size_in, size_out=None, size_h=None):
super().__init__()
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
self.fc_0 = nn.Linear(size_in, size_h)
self.fc_1 = nn.Linear(size_h, size_out)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Linear(size_in, size_out, bias=False)
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(x))
dx = self.fc_1(self.actvn(net))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'size_in': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
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_2(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = tmp0 + tmp3
tl.store(in_out_ptr0 + x2, tmp4, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = 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,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_relu_0[grid(256)](primals_1, buf0, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (64, 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, 4), (64, 16, 4, 1), 0)
del buf1
buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_1[grid(256)](buf2,
primals_3, buf5, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3)
buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf3
triton_poi_fused_add_2[grid(256)](buf4, primals_1, primals_5, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_1
del primals_5
return buf4, reinterpret_tensor(buf0, (64, 4), (4, 1), 0
), reinterpret_tensor(buf2, (64, 4), (4, 1), 0), primals_4, buf5
class ResnetBlockFCNew(nn.Module):
def __init__(self, size_in, size_out=None, size_h=None):
super().__init__()
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
self.fc_0 = nn.Linear(size_in, size_h)
self.fc_1 = nn.Linear(size_h, size_out)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Linear(size_in, size_out, bias=False)
nn.init.zeros_(self.fc_1.weight)
def forward(self, input_0):
primals_2 = self.fc_0.weight
primals_3 = self.fc_0.bias
primals_4 = self.fc_1.weight
primals_5 = self.fc_1.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DveloperY0115/texture_fields
|
ResnetBlockFC
| false
| 13,618
|
[
"MIT"
] | 78
|
28c277696e0a658ffff3496892810d5a0ef03f65
|
https://github.com/DveloperY0115/texture_fields/tree/28c277696e0a658ffff3496892810d5a0ef03f65
|
LinearPool
|
import torch
from torch import nn
class LinearPool(nn.Module):
def __init__(self):
super(LinearPool, self).__init__()
def forward(self, feat_map):
"""
Arguments:
feat_map(Tensor): tensor with shape (N, C, H, W)
return(Tensor): tensor with shape (N, C, 1, 1)
"""
EPSILON = 1e-07
_N, _C, _H, _W = feat_map.shape
sum_input = torch.sum(feat_map, dim=(-1, -2), keepdim=True)
sum_input += EPSILON
linear_weight = feat_map / sum_input
weighted_value = feat_map * linear_weight
return torch.sum(weighted_value, dim=(-1, -2), keepdim=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 import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_add_div_mul_sum_0(in_out_ptr0, in_ptr0, xnumel, rnumel,
XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tmp5 = 1e-07
tmp6 = tmp4 + tmp5
tmp7 = tmp0 / 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(in_out_ptr0 + x0, tmp12, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf1 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 1, 1), 0)
del buf0
get_raw_stream(0)
triton_per_fused_add_div_mul_sum_0[grid(16)](buf1, arg0_1, 16, 16,
XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
return buf1,
class LinearPoolNew(nn.Module):
def __init__(self):
super(LinearPoolNew, self).__init__()
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DavidChenL/Chexpert
|
LinearPool
| false
| 13,619
|
[
"Apache-2.0"
] | 202
|
0300057d3a51301cff35a65f79729436678b4a79
|
https://github.com/DavidChenL/Chexpert/tree/0300057d3a51301cff35a65f79729436678b4a79
|
hsigmoid
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class hsigmoid(nn.Module):
def forward(self, x):
out = F.relu6(x + 3, inplace=True) / 6
return out
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_add_div_hardtanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 3.0
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp5 = 6.0
tmp6 = triton_helpers.minimum(tmp4, tmp5)
tmp7 = 0.16666666666666666
tmp8 = tmp6 * tmp7
tl.store(out_ptr0 + x0, tmp8, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_hardtanh_0[grid(256)](arg0_1, buf0, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class hsigmoidNew(nn.Module):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Ecalose/dddd_trainer
|
hsigmoid
| false
| 13,620
|
[
"Apache-2.0"
] | 80
|
ef0c6b271cc2898403375f53f813481ffbf6b02c
|
https://github.com/Ecalose/dddd_trainer/tree/ef0c6b271cc2898403375f53f813481ffbf6b02c
|
ResnetBlockConv2d
|
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
def pixel_norm(x):
sigma = x.norm(dim=1, keepdim=True)
out = x / (sigma + 1e-05)
return out
class EqualizedLR(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
self._make_params()
def _make_params(self):
weight = self.module.weight
height = weight.data.shape[0]
width = weight.view(height, -1).data.shape[1]
del self.module._parameters['weight']
self.module.weight = None
self.weight = nn.Parameter(weight.data)
self.factor = np.sqrt(2 / width)
nn.init.normal_(self.weight)
self.bias = self.module.bias
self.module.bias = None
if self.bias is not None:
del self.module._parameters['bias']
nn.init.zeros_(self.bias)
def forward(self, *args, **kwargs):
self.module.weight = self.factor * self.weight
if self.bias is not None:
self.module.bias = 1.0 * self.bias
out = self.module.forward(*args, **kwargs)
self.module.weight = None
self.module.bias = None
return out
class ResnetBlockConv2d(nn.Module):
def __init__(self, f_in, f_out=None, f_hidden=None, is_bias=True, actvn
=F.relu, factor=1.0, eq_lr=False, pixel_norm=False):
super().__init__()
if f_out is None:
f_out = f_in
if f_hidden is None:
f_hidden = min(f_in, f_out)
self.f_in = f_in
self.f_hidden = f_hidden
self.f_out = f_out
self.factor = factor
self.eq_lr = eq_lr
self.use_pixel_norm = pixel_norm
self.actvn = actvn
self.conv_0 = nn.Conv2d(self.f_in, self.f_hidden, 3, stride=1,
padding=1)
self.conv_1 = nn.Conv2d(self.f_hidden, self.f_out, 3, stride=1,
padding=1, bias=is_bias)
if self.eq_lr:
self.conv_0 = EqualizedLR(self.conv_0)
self.conv_1 = EqualizedLR(self.conv_1)
if f_in == f_out:
self.shortcut = nn.Sequential()
else:
self.shortcut = nn.Conv2d(f_in, f_out, 1, bias=False)
if self.eq_lr:
self.shortcut = EqualizedLR(self.shortcut)
nn.init.zeros_(self.conv_1.weight)
def forward(self, x):
x_s = self.shortcut(x)
if self.use_pixel_norm:
x = pixel_norm(x)
dx = self.conv_0(self.actvn(x))
if self.use_pixel_norm:
dx = pixel_norm(dx)
dx = self.conv_1(self.actvn(dx))
out = x_s + self.factor * dx
return out
def _shortcut(self, x):
if self.learned_shortcut:
x_s = self.conv_s(x)
else:
x_s = x
return x_s
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'f_in': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tl.store(out_ptr0 + x0, tmp2, xmask)
@triton.jit
def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x3, tmp4, xmask)
@triton.jit
def triton_poi_fused_add_convolution_mul_2(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_out_ptr0 + x3, xmask)
tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = 1.0
tmp5 = tmp3 * tmp4
tmp6 = tmp0 + tmp5
tl.store(in_out_ptr0 + x3, tmp6, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_relu_0[grid(256)](primals_1, buf0, 256, XBLOCK=128,
num_warps=4, num_stages=1)
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_relu_1[grid(256)](buf2, primals_3, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1),
padding=(1, 1), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1))
buf4 = buf3
del buf3
triton_poi_fused_add_convolution_mul_2[grid(256)](buf4, primals_1,
primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
del primals_5
return buf4, primals_2, primals_4, buf0, buf2
def pixel_norm(x):
sigma = x.norm(dim=1, keepdim=True)
out = x / (sigma + 1e-05)
return out
class EqualizedLR(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
self._make_params()
def _make_params(self):
weight = self.module.weight
height = weight.data.shape[0]
width = weight.view(height, -1).data.shape[1]
del self.module._parameters['weight']
self.module.weight = None
self.weight = nn.Parameter(weight.data)
self.factor = np.sqrt(2 / width)
nn.init.normal_(self.weight)
self.bias = self.module.bias
self.module.bias = None
if self.bias is not None:
del self.module._parameters['bias']
nn.init.zeros_(self.bias)
def forward(self, *args, **kwargs):
self.module.weight = self.factor * self.weight
if self.bias is not None:
self.module.bias = 1.0 * self.bias
out = self.module.forward(*args, **kwargs)
self.module.weight = None
self.module.bias = None
return out
class ResnetBlockConv2dNew(nn.Module):
def __init__(self, f_in, f_out=None, f_hidden=None, is_bias=True, actvn
=F.relu, factor=1.0, eq_lr=False, pixel_norm=False):
super().__init__()
if f_out is None:
f_out = f_in
if f_hidden is None:
f_hidden = min(f_in, f_out)
self.f_in = f_in
self.f_hidden = f_hidden
self.f_out = f_out
self.factor = factor
self.eq_lr = eq_lr
self.use_pixel_norm = pixel_norm
self.actvn = actvn
self.conv_0 = nn.Conv2d(self.f_in, self.f_hidden, 3, stride=1,
padding=1)
self.conv_1 = nn.Conv2d(self.f_hidden, self.f_out, 3, stride=1,
padding=1, bias=is_bias)
if self.eq_lr:
self.conv_0 = EqualizedLR(self.conv_0)
self.conv_1 = EqualizedLR(self.conv_1)
if f_in == f_out:
self.shortcut = nn.Sequential()
else:
self.shortcut = nn.Conv2d(f_in, f_out, 1, bias=False)
if self.eq_lr:
self.shortcut = EqualizedLR(self.shortcut)
nn.init.zeros_(self.conv_1.weight)
def _shortcut(self, x):
if self.learned_shortcut:
x_s = self.conv_s(x)
else:
x_s = x
return x_s
def forward(self, input_0):
primals_2 = self.conv_0.weight
primals_3 = self.conv_0.bias
primals_4 = self.conv_1.weight
primals_5 = self.conv_1.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DveloperY0115/texture_fields
|
ResnetBlockConv2d
| false
| 13,621
|
[
"MIT"
] | 78
|
28c277696e0a658ffff3496892810d5a0ef03f65
|
https://github.com/DveloperY0115/texture_fields/tree/28c277696e0a658ffff3496892810d5a0ef03f65
|
OnnxGatherElements
|
import torch
from torch import nn
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxGatherElements(nn.Module, OnnxToTorchModule):
def __init__(self, axis: 'int'=0):
super().__init__()
self.axis = axis
def forward(self, input_tensor: 'torch.Tensor', indices: 'torch.Tensor'
) ->torch.Tensor:
return torch.gather(input_tensor, dim=self.axis, index=indices)
def get_inputs():
return [torch.ones([4], dtype=torch.int64), torch.ones([4], dtype=torch
.int64)]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch 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_gather_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([XBLOCK], 4, tl.int32)
tmp2 = tmp0 + tmp1
tmp3 = tmp0 < 0
tmp4 = tl.where(tmp3, tmp2, tmp0)
tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
'index out of bounds: 0 <= tmp4 < 4')
tmp6 = tl.load(in_ptr1 + tmp4, xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x0, tmp6, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4,), (1,))
assert_size_stride(arg1_1, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4,), (1,), torch.int64)
get_raw_stream(0)
triton_poi_fused_gather_0[grid(4)](arg0_1, arg1_1, buf0, 4, XBLOCK=
4, num_warps=1, num_stages=1)
del arg0_1
del arg1_1
return buf0,
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxGatherElementsNew(nn.Module, OnnxToTorchModule):
def __init__(self, axis: 'int'=0):
super().__init__()
self.axis = axis
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxGatherElements
| false
| 13,622
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
hswish
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class hswish(nn.Module):
def forward(self, x):
out = x * F.relu6(x + 3, inplace=True) / 6
return out
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_add_div_hardtanh_mul_0(in_ptr0, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 3.0
tmp2 = tmp0 + tmp1
tmp3 = 0.0
tmp4 = triton_helpers.maximum(tmp2, tmp3)
tmp5 = 6.0
tmp6 = triton_helpers.minimum(tmp4, tmp5)
tmp7 = tmp0 * tmp6
tmp8 = 0.16666666666666666
tmp9 = tmp7 * tmp8
tl.store(out_ptr0 + x0, tmp9, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_hardtanh_mul_0[grid(256)](arg0_1, buf0,
256, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class hswishNew(nn.Module):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Ecalose/dddd_trainer
|
hswish
| false
| 13,623
|
[
"Apache-2.0"
] | 80
|
ef0c6b271cc2898403375f53f813481ffbf6b02c
|
https://github.com/Ecalose/dddd_trainer/tree/ef0c6b271cc2898403375f53f813481ffbf6b02c
|
ConvBlock
|
import copy
import torch
from torch import nn
import torch.utils.data
def get_activation(activation):
if isinstance(activation, str):
if activation == 'relu':
return nn.ReLU()
elif activation == 'leaky':
return nn.LeakyReLU(negative_slope=0.1)
elif activation == 'prelu':
return nn.PReLU(num_parameters=1)
elif activation == 'rrelu':
return nn.RReLU()
elif activation == 'silu':
return nn.SiLU()
elif activation == 'lin':
return nn.Identity()
else:
return copy.deepcopy(activation)
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def get_normalization(normtype: 'str', num_channels: 'int', dim: 'int'=3):
"""Chooses an implementation for a batch normalization layer."""
if normtype is None or normtype == 'none':
return nn.Identity()
elif normtype.startswith('group'):
if normtype == 'group':
num_groups = 8
elif len(normtype) > len('group') and normtype[len('group'):].isdigit(
):
num_groups = int(normtype[len('group'):])
else:
raise ValueError(
f'normtype "{normtype}" not understood. It should be "group<G>", where <G> is the number of groups.'
)
return nn.GroupNorm(num_groups=num_groups, num_channels=num_channels)
elif normtype == 'instance':
if dim == 3:
return nn.InstanceNorm3d(num_channels)
elif dim == 2:
return nn.InstanceNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
elif normtype == 'batch':
if dim == 3:
return nn.BatchNorm3d(num_channels)
elif dim == 2:
return nn.BatchNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
else:
raise ValueError(
f"""Unknown normalization type "{normtype}".
Valid choices are "batch", "instance", "group" or "group<G>",where <G> is the number of groups."""
)
def get_padding(conv_mode, kernel_size):
if conv_mode == 'valid' or kernel_size == 1:
return 0
elif conv_mode == 'same' and kernel_size == 3:
return 1
else:
raise NotImplementedError(
f'conv_mode {conv_mode} with kernel_size {kernel_size} unsupported.'
)
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, planar=
False, activation='relu', normalization=None, dim=3, conv_mode=
'same', residual=False):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.normalization = normalization
self.conv_mode = conv_mode
self.activation = activation
self.residual = residual
self.dim = dim
padding = get_padding(conv_mode, kernel_size)
if planar:
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
conv_class = get_conv(dim)
self.conv1 = conv_class(in_channels, out_channels, kernel_size=
kernel_size, padding=padding)
self.norm1 = get_normalization(normalization, self.out_channels,
dim=dim)
self.act1 = get_activation(activation)
self.conv2 = conv_class(out_channels, out_channels, kernel_size=
kernel_size, padding=padding)
self.norm2 = get_normalization(normalization, self.out_channels,
dim=dim)
self.act2 = get_activation(activation)
if self.residual and self.in_channels != self.out_channels:
self.proj = conv_class(in_channels, out_channels, kernel_size=1)
else:
self.proj = nn.Identity()
def forward(self, inp):
y = self.conv1(inp)
y = self.norm1(y)
y = self.act1(y)
y = self.conv2(y)
if self.residual:
y += self.proj(inp)
y = self.norm2(y)
y = self.act2(y)
return y
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 copy
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_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
x1 = xindex // 64
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1,
4, 4, 4, 4), (256, 64, 16, 4, 1), 0), primals_1, stride=(1, 1,
1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
buf5 = 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, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = extern_kernels.convolution(reinterpret_tensor(buf1, (1, 4, 4,
4, 4), (0, 64, 16, 4, 1), 0), primals_4, stride=(1, 1, 1),
padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf2, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf2
buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
triton_poi_fused_relu_threshold_backward_0[grid(256)](buf3,
primals_5, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
del primals_5
return buf3, primals_1, primals_4, reinterpret_tensor(primals_3, (1, 4,
4, 4, 4), (256, 64, 16, 4, 1), 0), reinterpret_tensor(buf1, (1, 4,
4, 4, 4), (256, 64, 16, 4, 1), 0), buf4, buf5
def get_activation(activation):
if isinstance(activation, str):
if activation == 'relu':
return nn.ReLU()
elif activation == 'leaky':
return nn.LeakyReLU(negative_slope=0.1)
elif activation == 'prelu':
return nn.PReLU(num_parameters=1)
elif activation == 'rrelu':
return nn.RReLU()
elif activation == 'silu':
return nn.SiLU()
elif activation == 'lin':
return nn.Identity()
else:
return copy.deepcopy(activation)
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def get_normalization(normtype: 'str', num_channels: 'int', dim: 'int'=3):
"""Chooses an implementation for a batch normalization layer."""
if normtype is None or normtype == 'none':
return nn.Identity()
elif normtype.startswith('group'):
if normtype == 'group':
num_groups = 8
elif len(normtype) > len('group') and normtype[len('group'):].isdigit(
):
num_groups = int(normtype[len('group'):])
else:
raise ValueError(
f'normtype "{normtype}" not understood. It should be "group<G>", where <G> is the number of groups.'
)
return nn.GroupNorm(num_groups=num_groups, num_channels=num_channels)
elif normtype == 'instance':
if dim == 3:
return nn.InstanceNorm3d(num_channels)
elif dim == 2:
return nn.InstanceNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
elif normtype == 'batch':
if dim == 3:
return nn.BatchNorm3d(num_channels)
elif dim == 2:
return nn.BatchNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
else:
raise ValueError(
f"""Unknown normalization type "{normtype}".
Valid choices are "batch", "instance", "group" or "group<G>",where <G> is the number of groups."""
)
def get_padding(conv_mode, kernel_size):
if conv_mode == 'valid' or kernel_size == 1:
return 0
elif conv_mode == 'same' and kernel_size == 3:
return 1
else:
raise NotImplementedError(
f'conv_mode {conv_mode} with kernel_size {kernel_size} unsupported.'
)
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
class ConvBlockNew(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, planar=
False, activation='relu', normalization=None, dim=3, conv_mode=
'same', residual=False):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.normalization = normalization
self.conv_mode = conv_mode
self.activation = activation
self.residual = residual
self.dim = dim
padding = get_padding(conv_mode, kernel_size)
if planar:
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
conv_class = get_conv(dim)
self.conv1 = conv_class(in_channels, out_channels, kernel_size=
kernel_size, padding=padding)
self.norm1 = get_normalization(normalization, self.out_channels,
dim=dim)
self.act1 = get_activation(activation)
self.conv2 = conv_class(out_channels, out_channels, kernel_size=
kernel_size, padding=padding)
self.norm2 = get_normalization(normalization, self.out_channels,
dim=dim)
self.act2 = get_activation(activation)
if self.residual and self.in_channels != self.out_channels:
self.proj = conv_class(in_channels, out_channels, kernel_size=1)
else:
self.proj = nn.Identity()
def forward(self, input_0):
primals_1 = self.conv1.weight
primals_2 = self.conv1.bias
primals_4 = self.conv2.weight
primals_5 = self.conv2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ELEKTRONN/elektronn3
|
ConvBlock
| false
| 13,624
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
UpsampleConvLayer
|
import torch
class UpsampleConvLayer(torch.nn.Module):
"""UpsampleConvLayer
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
reflection_padding = 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 = torch.nn.functional.interpolate(x_in, mode='nearest',
scale_factor=self.upsample)
out = self.reflection_pad(x_in)
out = self.conv2d(out)
return out
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 math as tl_math
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_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_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 400
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 25 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_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
triton_poi_fused_convolution_1[grid(400)](buf2, primals_3, 400,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_3
return buf2, primals_2, buf0
class UpsampleConvLayerNew(torch.nn.Module):
"""UpsampleConvLayer
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(UpsampleConvLayerNew, self).__init__()
self.upsample = upsample
reflection_padding = 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, 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]
|
EdenBD/MultiModalStory-demo
|
UpsampleConvLayer
| false
| 13,625
|
[
"Apache-2.0"
] | 154
|
5e95e2aca766ca7c850e8db4973b8d51dfdba7f8
|
https://github.com/EdenBD/MultiModalStory-demo/tree/5e95e2aca766ca7c850e8db4973b8d51dfdba7f8
|
OnnxPow
|
import torch
from torch import nn
from typing import Optional
def old_style_broadcast(first: 'torch.Tensor', second: 'torch.Tensor', axis:
'int') ->torch.Tensor:
rank = len(first.shape)
axis = axis + rank if axis < 0 else axis
second_shape = [1] * axis + list(second.shape)
second_shape = second_shape + [1] * (rank - len(second_shape))
return second.view(second_shape)
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxPow(nn.Module, OnnxToTorchModule):
def __init__(self, broadcast: 'Optional[int]'=None, axis:
'Optional[int]'=None):
super().__init__()
self.axis = axis
self.broadcast = broadcast
def forward(self, input_tensor: 'torch.Tensor', exponent: 'torch.Tensor'
) ->torch.Tensor:
if self.broadcast == 1 and self.axis is not None:
exponent = old_style_broadcast(input_tensor, exponent, self.axis)
return torch.pow(input_tensor, exponent)
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
from torch import nn
from typing import Optional
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_pow_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 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 = libdevice.pow(tmp0, tmp1)
tl.store(out_ptr0 + x0, tmp2, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_pow_0[grid(256)](arg1_1, arg0_1, buf0, 256, XBLOCK
=256, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
return buf0,
def old_style_broadcast(first: 'torch.Tensor', second: 'torch.Tensor', axis:
'int') ->torch.Tensor:
rank = len(first.shape)
axis = axis + rank if axis < 0 else axis
second_shape = [1] * axis + list(second.shape)
second_shape = second_shape + [1] * (rank - len(second_shape))
return second.view(second_shape)
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxPowNew(nn.Module, OnnxToTorchModule):
def __init__(self, broadcast: 'Optional[int]'=None, axis:
'Optional[int]'=None):
super().__init__()
self.axis = axis
self.broadcast = broadcast
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxPow
| false
| 13,626
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
DownConv
|
import copy
import torch
from torch import nn
import torch.utils.data
def get_activation(activation):
if isinstance(activation, str):
if activation == 'relu':
return nn.ReLU()
elif activation == 'leaky':
return nn.LeakyReLU(negative_slope=0.1)
elif activation == 'prelu':
return nn.PReLU(num_parameters=1)
elif activation == 'rrelu':
return nn.RReLU()
elif activation == 'silu':
return nn.SiLU()
elif activation == 'lin':
return nn.Identity()
else:
return copy.deepcopy(activation)
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def get_normalization(normtype: 'str', num_channels: 'int', dim: 'int'=3):
"""Chooses an implementation for a batch normalization layer."""
if normtype is None or normtype == 'none':
return nn.Identity()
elif normtype.startswith('group'):
if normtype == 'group':
num_groups = 8
elif len(normtype) > len('group') and normtype[len('group'):].isdigit(
):
num_groups = int(normtype[len('group'):])
else:
raise ValueError(
f'normtype "{normtype}" not understood. It should be "group<G>", where <G> is the number of groups.'
)
return nn.GroupNorm(num_groups=num_groups, num_channels=num_channels)
elif normtype == 'instance':
if dim == 3:
return nn.InstanceNorm3d(num_channels)
elif dim == 2:
return nn.InstanceNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
elif normtype == 'batch':
if dim == 3:
return nn.BatchNorm3d(num_channels)
elif dim == 2:
return nn.BatchNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
else:
raise ValueError(
f"""Unknown normalization type "{normtype}".
Valid choices are "batch", "instance", "group" or "group<G>",where <G> is the number of groups."""
)
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
def get_maxpool(dim=3):
"""Chooses an implementation for a max-pooling layer."""
if dim == 3:
return nn.MaxPool3d
elif dim == 2:
return nn.MaxPool2d
else:
raise ValueError('dim has to be 2 or 3')
def conv3(in_channels, out_channels, kernel_size=3, stride=1, padding=1,
bias=True, planar=False, dim=3):
"""Returns an appropriate spatial convolution layer, depending on args.
- dim=2: Conv2d with 3x3 kernel
- dim=3 and planar=False: Conv3d with 3x3x3 kernel
- dim=3 and planar=True: Conv3d with 1x3x3 kernel
"""
if planar:
stride = planar_kernel(stride)
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
return get_conv(dim)(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=bias)
class DownConv(nn.Module):
"""
A helper Module that performs 2 convolutions and 1 MaxPool.
A ReLU activation follows each convolution.
"""
def __init__(self, in_channels, out_channels, pooling=True, planar=
False, activation='relu', normalization=None, full_norm=True, dim=3,
conv_mode='same'):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.pooling = pooling
self.normalization = normalization
self.dim = dim
padding = 1 if 'same' in conv_mode else 0
self.conv1 = conv3(self.in_channels, self.out_channels, planar=
planar, dim=dim, padding=padding)
self.conv2 = conv3(self.out_channels, self.out_channels, planar=
planar, dim=dim, padding=padding)
if self.pooling:
kernel_size = 2
if planar:
kernel_size = planar_kernel(kernel_size)
self.pool = get_maxpool(dim)(kernel_size=kernel_size, ceil_mode
=True)
self.pool_ks = kernel_size
else:
self.pool = nn.Identity()
self.pool_ks = -123
self.act1 = get_activation(activation)
self.act2 = get_activation(activation)
if full_norm:
self.norm0 = get_normalization(normalization, self.out_channels,
dim=dim)
else:
self.norm0 = nn.Identity()
self.norm1 = get_normalization(normalization, self.out_channels,
dim=dim)
def forward(self, x):
y = self.conv1(x)
y = self.norm0(y)
y = self.act1(y)
y = self.conv2(y)
y = self.norm1(y)
y = self.act2(y)
before_pool = y
y = self.pool(y)
return y, before_pool
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 copy
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_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
x1 = xindex // 64
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 64
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1,
4, 4, 4, 4), (256, 64, 16, 4, 1), 0), primals_1, stride=(1, 1,
1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
buf1 = 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
buf2 = extern_kernels.convolution(reinterpret_tensor(buf1, (1, 4, 4,
4, 4), (0, 64, 16, 4, 1), 0), primals_4, stride=(1, 1, 1),
padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
output_padding=(0, 0, 0), groups=1, bias=None)
assert_size_stride(buf2, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf2
triton_poi_fused_relu_1[grid(256)](buf3, primals_5, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del primals_5
buf4 = torch.ops.aten.max_pool3d_with_indices.default(buf3, [2, 2,
2], [2, 2, 2], [0, 0, 0], [1, 1, 1], True)
buf5 = buf4[0]
buf6 = buf4[1]
del buf4
return buf5, buf3, primals_1, primals_4, reinterpret_tensor(primals_3,
(1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), reinterpret_tensor(buf1,
(1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), buf3, buf6, buf7
def get_activation(activation):
if isinstance(activation, str):
if activation == 'relu':
return nn.ReLU()
elif activation == 'leaky':
return nn.LeakyReLU(negative_slope=0.1)
elif activation == 'prelu':
return nn.PReLU(num_parameters=1)
elif activation == 'rrelu':
return nn.RReLU()
elif activation == 'silu':
return nn.SiLU()
elif activation == 'lin':
return nn.Identity()
else:
return copy.deepcopy(activation)
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def get_normalization(normtype: 'str', num_channels: 'int', dim: 'int'=3):
"""Chooses an implementation for a batch normalization layer."""
if normtype is None or normtype == 'none':
return nn.Identity()
elif normtype.startswith('group'):
if normtype == 'group':
num_groups = 8
elif len(normtype) > len('group') and normtype[len('group'):].isdigit(
):
num_groups = int(normtype[len('group'):])
else:
raise ValueError(
f'normtype "{normtype}" not understood. It should be "group<G>", where <G> is the number of groups.'
)
return nn.GroupNorm(num_groups=num_groups, num_channels=num_channels)
elif normtype == 'instance':
if dim == 3:
return nn.InstanceNorm3d(num_channels)
elif dim == 2:
return nn.InstanceNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
elif normtype == 'batch':
if dim == 3:
return nn.BatchNorm3d(num_channels)
elif dim == 2:
return nn.BatchNorm2d(num_channels)
else:
raise ValueError('dim has to be 2 or 3')
else:
raise ValueError(
f"""Unknown normalization type "{normtype}".
Valid choices are "batch", "instance", "group" or "group<G>",where <G> is the number of groups."""
)
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
def get_maxpool(dim=3):
"""Chooses an implementation for a max-pooling layer."""
if dim == 3:
return nn.MaxPool3d
elif dim == 2:
return nn.MaxPool2d
else:
raise ValueError('dim has to be 2 or 3')
def conv3(in_channels, out_channels, kernel_size=3, stride=1, padding=1,
bias=True, planar=False, dim=3):
"""Returns an appropriate spatial convolution layer, depending on args.
- dim=2: Conv2d with 3x3 kernel
- dim=3 and planar=False: Conv3d with 3x3x3 kernel
- dim=3 and planar=True: Conv3d with 1x3x3 kernel
"""
if planar:
stride = planar_kernel(stride)
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
return get_conv(dim)(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=bias)
class DownConvNew(nn.Module):
"""
A helper Module that performs 2 convolutions and 1 MaxPool.
A ReLU activation follows each convolution.
"""
def __init__(self, in_channels, out_channels, pooling=True, planar=
False, activation='relu', normalization=None, full_norm=True, dim=3,
conv_mode='same'):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.pooling = pooling
self.normalization = normalization
self.dim = dim
padding = 1 if 'same' in conv_mode else 0
self.conv1 = conv3(self.in_channels, self.out_channels, planar=
planar, dim=dim, padding=padding)
self.conv2 = conv3(self.out_channels, self.out_channels, planar=
planar, dim=dim, padding=padding)
if self.pooling:
kernel_size = 2
if planar:
kernel_size = planar_kernel(kernel_size)
self.pool = get_maxpool(dim)(kernel_size=kernel_size, ceil_mode
=True)
self.pool_ks = kernel_size
else:
self.pool = nn.Identity()
self.pool_ks = -123
self.act1 = get_activation(activation)
self.act2 = get_activation(activation)
if full_norm:
self.norm0 = get_normalization(normalization, self.out_channels,
dim=dim)
else:
self.norm0 = nn.Identity()
self.norm1 = get_normalization(normalization, self.out_channels,
dim=dim)
def forward(self, input_0):
primals_1 = self.conv1.weight
primals_2 = self.conv1.bias
primals_4 = self.conv2.weight
primals_5 = self.conv2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0], output[1]
|
ELEKTRONN/elektronn3
|
DownConv
| false
| 13,627
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
ResnetBlockPointwise
|
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
class EqualizedLR(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
self._make_params()
def _make_params(self):
weight = self.module.weight
height = weight.data.shape[0]
width = weight.view(height, -1).data.shape[1]
del self.module._parameters['weight']
self.module.weight = None
self.weight = nn.Parameter(weight.data)
self.factor = np.sqrt(2 / width)
nn.init.normal_(self.weight)
self.bias = self.module.bias
self.module.bias = None
if self.bias is not None:
del self.module._parameters['bias']
nn.init.zeros_(self.bias)
def forward(self, *args, **kwargs):
self.module.weight = self.factor * self.weight
if self.bias is not None:
self.module.bias = 1.0 * self.bias
out = self.module.forward(*args, **kwargs)
self.module.weight = None
self.module.bias = None
return out
class ResnetBlockPointwise(nn.Module):
def __init__(self, f_in, f_out=None, f_hidden=None, is_bias=True, actvn
=F.relu, factor=1.0, eq_lr=False):
super().__init__()
if f_out is None:
f_out = f_in
if f_hidden is None:
f_hidden = min(f_in, f_out)
self.f_in = f_in
self.f_hidden = f_hidden
self.f_out = f_out
self.factor = factor
self.eq_lr = eq_lr
self.actvn = actvn
self.conv_0 = nn.Conv1d(f_in, f_hidden, 1)
self.conv_1 = nn.Conv1d(f_hidden, f_out, 1, bias=is_bias)
if self.eq_lr:
self.conv_0 = EqualizedLR(self.conv_0)
self.conv_1 = EqualizedLR(self.conv_1)
if f_in == f_out:
self.shortcut = nn.Sequential()
else:
self.shortcut = nn.Conv1d(f_in, f_out, 1, bias=False)
if self.eq_lr:
self.shortcut = EqualizedLR(self.shortcut)
nn.init.zeros_(self.conv_1.weight)
def forward(self, x):
net = self.conv_0(self.actvn(x))
dx = self.conv_1(self.actvn(net))
x_s = self.shortcut(x)
return x_s + self.factor * dx
def get_inputs():
return [torch.rand([4, 4])]
def get_init_inputs():
return [[], {'f_in': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import numpy as np
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_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tl.store(out_ptr0 + x0, tmp2, 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
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_add_mul_2(in_out_ptr0, in_ptr0, in_ptr1, xnumel,
XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_out_ptr0 + x2, xmask)
tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp4 = 1.0
tmp5 = tmp3 * tmp4
tmp6 = tmp0 + tmp5
tl.store(in_out_ptr0 + x2, tmp6, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1))
assert_size_stride(primals_5, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_relu_0[grid(16)](primals_1, buf0, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (1, 4, 4
), (0, 4, 1), 0), primals_2, stride=(1,), padding=(0,),
dilation=(1,), transposed=False, output_padding=(0,), groups=1,
bias=None)
assert_size_stride(buf1, (1, 4, 4), (16, 4, 1))
buf2 = reinterpret_tensor(buf1, (4, 4), (4, 1), 0)
del buf1
buf5 = empty_strided_cuda((4, 4), (4, 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 = extern_kernels.convolution(reinterpret_tensor(buf2, (1, 4, 4
), (0, 4, 1), 0), primals_4, stride=(1,), padding=(0,),
dilation=(1,), transposed=False, output_padding=(0,), groups=1,
bias=None)
assert_size_stride(buf3, (1, 4, 4), (16, 4, 1))
buf4 = reinterpret_tensor(buf3, (4, 4), (4, 1), 0)
del buf3
triton_poi_fused_add_mul_2[grid(16)](buf4, primals_1, primals_5, 16,
XBLOCK=16, num_warps=1, num_stages=1)
del primals_1
del primals_5
return buf4, primals_2, primals_4, reinterpret_tensor(buf0, (1, 4, 4),
(16, 4, 1), 0), reinterpret_tensor(buf2, (1, 4, 4), (16, 4, 1), 0
), buf5
class EqualizedLR(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
self._make_params()
def _make_params(self):
weight = self.module.weight
height = weight.data.shape[0]
width = weight.view(height, -1).data.shape[1]
del self.module._parameters['weight']
self.module.weight = None
self.weight = nn.Parameter(weight.data)
self.factor = np.sqrt(2 / width)
nn.init.normal_(self.weight)
self.bias = self.module.bias
self.module.bias = None
if self.bias is not None:
del self.module._parameters['bias']
nn.init.zeros_(self.bias)
def forward(self, *args, **kwargs):
self.module.weight = self.factor * self.weight
if self.bias is not None:
self.module.bias = 1.0 * self.bias
out = self.module.forward(*args, **kwargs)
self.module.weight = None
self.module.bias = None
return out
class ResnetBlockPointwiseNew(nn.Module):
def __init__(self, f_in, f_out=None, f_hidden=None, is_bias=True, actvn
=F.relu, factor=1.0, eq_lr=False):
super().__init__()
if f_out is None:
f_out = f_in
if f_hidden is None:
f_hidden = min(f_in, f_out)
self.f_in = f_in
self.f_hidden = f_hidden
self.f_out = f_out
self.factor = factor
self.eq_lr = eq_lr
self.actvn = actvn
self.conv_0 = nn.Conv1d(f_in, f_hidden, 1)
self.conv_1 = nn.Conv1d(f_hidden, f_out, 1, bias=is_bias)
if self.eq_lr:
self.conv_0 = EqualizedLR(self.conv_0)
self.conv_1 = EqualizedLR(self.conv_1)
if f_in == f_out:
self.shortcut = nn.Sequential()
else:
self.shortcut = nn.Conv1d(f_in, f_out, 1, bias=False)
if self.eq_lr:
self.shortcut = EqualizedLR(self.shortcut)
nn.init.zeros_(self.conv_1.weight)
def forward(self, input_0):
primals_2 = self.conv_0.weight
primals_3 = self.conv_0.bias
primals_4 = self.conv_1.weight
primals_5 = self.conv_1.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
DveloperY0115/texture_fields
|
ResnetBlockPointwise
| false
| 13,628
|
[
"MIT"
] | 78
|
28c277696e0a658ffff3496892810d5a0ef03f65
|
https://github.com/DveloperY0115/texture_fields/tree/28c277696e0a658ffff3496892810d5a0ef03f65
|
ResizeConv
|
import torch
from torch import nn
import torch.utils.data
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
def conv1(in_channels, out_channels, dim=3):
"""Returns a 1x1 or 1x1x1 convolution, depending on dim"""
return get_conv(dim)(in_channels, out_channels, kernel_size=1)
def conv3(in_channels, out_channels, kernel_size=3, stride=1, padding=1,
bias=True, planar=False, dim=3):
"""Returns an appropriate spatial convolution layer, depending on args.
- dim=2: Conv2d with 3x3 kernel
- dim=3 and planar=False: Conv3d with 3x3x3 kernel
- dim=3 and planar=True: Conv3d with 1x3x3 kernel
"""
if planar:
stride = planar_kernel(stride)
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
return get_conv(dim)(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=bias)
class ResizeConv(nn.Module):
"""Upsamples by 2x and applies a convolution.
This is meant as a replacement for transposed convolution to avoid
checkerboard artifacts. See
- https://distill.pub/2016/deconv-checkerboard/
- https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/190
"""
def __init__(self, in_channels, out_channels, kernel_size=3, planar=
False, dim=3, upsampling_mode='nearest'):
super().__init__()
self.upsampling_mode = upsampling_mode
self.scale_factor = 2
if dim == 3 and planar:
self.scale_factor = planar_kernel(self.scale_factor)
self.dim = dim
self.upsample = nn.Upsample(scale_factor=self.scale_factor, mode=
self.upsampling_mode)
if kernel_size == 3:
self.conv = conv3(in_channels, out_channels, padding=1, planar=
planar, dim=dim)
elif kernel_size == 1:
self.conv = conv1(in_channels, out_channels, dim=dim)
else:
raise ValueError(
f'kernel_size={kernel_size} is not supported. Choose 1 or 3.')
def forward(self, x):
return self.conv(self.upsample(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 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__unsafe_index_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 8 % 8
x0 = xindex % 8
x2 = xindex // 64
x4 = xindex
tmp0 = x1
tmp1 = tmp0.to(tl.float32)
tmp2 = 0.5
tmp3 = tmp1 * tmp2
tmp4 = tmp3.to(tl.int32)
tmp5 = x0
tmp6 = tmp5.to(tl.float32)
tmp7 = tmp6 * tmp2
tmp8 = tmp7.to(tl.int32)
tmp9 = tl.load(in_ptr0 + (tmp8 + 4 * tmp4 + 16 * x2), xmask,
eviction_policy='evict_last')
tl.store(out_ptr0 + x4, tmp9, xmask)
@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 1024
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 256
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, 3, 3, 3), (108, 27, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__unsafe_index_0[grid(1024)](primals_1, buf0, 1024,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (1, 4, 4,
8, 8), (0, 256, 64, 8, 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(buf1, (1, 4, 4, 8, 8), (1024, 256, 64, 8, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(1024)](buf2, primals_3, 1024,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
return reinterpret_tensor(buf2, (4, 4, 8, 8), (256, 64, 8, 1), 0
), primals_2, reinterpret_tensor(buf0, (1, 4, 4, 8, 8), (1024, 256,
64, 8, 1), 0)
def get_conv(dim=3):
"""Chooses an implementation for a convolution layer."""
if dim == 3:
return nn.Conv3d
elif dim == 2:
return nn.Conv2d
else:
raise ValueError('dim has to be 2 or 3')
def planar_kernel(x):
"""Returns a "planar" kernel shape (e.g. for 2D convolution in 3D space)
that doesn't consider the first spatial dim (D)."""
if isinstance(x, int):
return 1, x, x
else:
return x
def planar_pad(x):
"""Returns a "planar" padding shape that doesn't pad along the first spatial dim (D)."""
if isinstance(x, int):
return 0, x, x
else:
return x
def conv1(in_channels, out_channels, dim=3):
"""Returns a 1x1 or 1x1x1 convolution, depending on dim"""
return get_conv(dim)(in_channels, out_channels, kernel_size=1)
def conv3(in_channels, out_channels, kernel_size=3, stride=1, padding=1,
bias=True, planar=False, dim=3):
"""Returns an appropriate spatial convolution layer, depending on args.
- dim=2: Conv2d with 3x3 kernel
- dim=3 and planar=False: Conv3d with 3x3x3 kernel
- dim=3 and planar=True: Conv3d with 1x3x3 kernel
"""
if planar:
stride = planar_kernel(stride)
padding = planar_pad(padding)
kernel_size = planar_kernel(kernel_size)
return get_conv(dim)(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=bias)
class ResizeConvNew(nn.Module):
"""Upsamples by 2x and applies a convolution.
This is meant as a replacement for transposed convolution to avoid
checkerboard artifacts. See
- https://distill.pub/2016/deconv-checkerboard/
- https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/190
"""
def __init__(self, in_channels, out_channels, kernel_size=3, planar=
False, dim=3, upsampling_mode='nearest'):
super().__init__()
self.upsampling_mode = upsampling_mode
self.scale_factor = 2
if dim == 3 and planar:
self.scale_factor = planar_kernel(self.scale_factor)
self.dim = dim
self.upsample = nn.Upsample(scale_factor=self.scale_factor, mode=
self.upsampling_mode)
if kernel_size == 3:
self.conv = conv3(in_channels, out_channels, padding=1, planar=
planar, dim=dim)
elif kernel_size == 1:
self.conv = conv1(in_channels, out_channels, dim=dim)
else:
raise ValueError(
f'kernel_size={kernel_size} is not supported. Choose 1 or 3.')
def forward(self, input_0):
primals_2 = self.conv.weight
primals_3 = self.conv.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ELEKTRONN/elektronn3
|
ResizeConv
| false
| 13,629
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
GaussionConvF
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class GaussionConvF(nn.Module):
"""The first layer in `RobustGCN` that conver node features to distribution (mean, var)"""
def __init__(self, in_features, out_features, bias=False, gamma=1.0):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
self.gamma = gamma
def reset_parameters(self):
self.w.reset_parameters()
def forward(self, x, adj_mean, adj_var):
h = self.w(x)
mean = F.elu(h)
var = F.relu(h)
attention = torch.exp(-self.gamma * var)
mean = adj_mean.mm(mean * attention)
var = adj_var.mm(var * attention * attention)
return mean, var
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, 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_elu_exp_mul_relu_0(in_ptr0, out_ptr0, out_ptr1,
out_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.0
tmp2 = tmp0 > tmp1
tmp3 = 1.0
tmp4 = tmp0 * tmp3
tmp5 = libdevice.expm1(tmp4)
tmp6 = tmp5 * tmp3
tmp7 = tl.where(tmp2, tmp4, tmp6)
tmp8 = tl.full([1], 0, tl.int32)
tmp9 = triton_helpers.maximum(tmp8, tmp0)
tmp10 = -1.0
tmp11 = tmp9 * tmp10
tmp12 = tl_math.exp(tmp11)
tmp13 = tmp7 * tmp12
tmp14 = tmp9 * tmp12
tmp15 = tmp14 * tmp12
tl.store(out_ptr0 + x0, tmp13, xmask)
tl.store(out_ptr1 + x0, tmp14, xmask)
tl.store(out_ptr2 + x0, tmp15, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4),
(1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_elu_exp_mul_relu_0[grid(16)](buf0, buf1, buf3,
buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf1, out=buf2)
buf5 = buf1
del buf1
extern_kernels.mm(primals_4, buf4, out=buf5)
del buf4
return buf2, buf5, primals_2, buf0, buf3, reinterpret_tensor(primals_4,
(4, 4), (1, 4), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0)
class GaussionConvFNew(nn.Module):
"""The first layer in `RobustGCN` that conver node features to distribution (mean, var)"""
def __init__(self, in_features, out_features, bias=False, gamma=1.0):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
self.gamma = gamma
def reset_parameters(self):
self.w.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def forward(self, input_0, input_1, input_2):
primals_1 = self.w.weight
primals_2 = 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]
|
EdisonLeeeee/GraphGallery
|
GaussionConvF
| false
| 13,630
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
OnnxSqrt
|
import torch
from torch import nn
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxSqrt(nn.Module, OnnxToTorchModule):
def forward(self, input_tensor: 'torch.Tensor') ->torch.Tensor:
return torch.sqrt(input_tensor)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_sqrt_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp1 = libdevice.sqrt(tmp0)
tl.store(out_ptr0 + x0, tmp1, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_sqrt_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del arg0_1
return buf0,
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxSqrtNew(nn.Module, OnnxToTorchModule):
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxSqrt
| false
| 13,631
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
OnnxGeneralLinear
|
import torch
from torch import nn
import torch.nn.functional as F
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxGeneralLinear(nn.Linear, OnnxToTorchModule):
"""General Linear layer with functionality of ONNX GEMM node.
For additional info https://github.com/onnx/onnx/blob/master/docs/Operators.md#Gemm
"""
def __init__(self, in_features: 'int', out_features: 'int', bias:
'bool', trans_a: 'int'):
super().__init__(in_features=in_features, out_features=out_features,
bias=bias)
self.trans_a = trans_a
def forward(self, input_tensor: 'torch.Tensor') ->torch.Tensor:
input_tensor = torch.transpose(input_tensor, 0, 1
) if self.trans_a != 0 else input_tensor
return F.linear(input_tensor, self.weight, self.bias)
@classmethod
def maybe_create_simple_linear(cls, in_features: 'int', out_features:
'int', bias: 'bool', trans_a: 'int'):
if trans_a == 0:
return nn.Linear(in_features=in_features, out_features=
out_features, bias=bias)
return OnnxGeneralLinear(in_features, out_features, bias, trans_a)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4, 'bias': 4, 'trans_a': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16 % 4
x2 = xindex // 64
x3 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2 + 64 * x1), xmask)
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_poi_fused_add_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_clone_0[grid(256)](primals_1, buf0, 256, XBLOCK=
128, num_warps=4, num_stages=1)
del primals_1
buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (64, 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, 4), (64, 16, 4, 1), 0)
del buf1
triton_poi_fused_add_1[grid(256)](buf2, primals_3, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del primals_3
return buf2, reinterpret_tensor(buf0, (64, 4), (4, 1), 0)
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxGeneralLinearNew(nn.Linear, OnnxToTorchModule):
"""General Linear layer with functionality of ONNX GEMM node.
For additional info https://github.com/onnx/onnx/blob/master/docs/Operators.md#Gemm
"""
def __init__(self, in_features: 'int', out_features: 'int', bias:
'bool', trans_a: 'int'):
super().__init__(in_features=in_features, out_features=out_features,
bias=bias)
self.trans_a = trans_a
@classmethod
def maybe_create_simple_linear(cls, in_features: 'int', out_features:
'int', bias: 'bool', trans_a: 'int'):
if trans_a == 0:
return nn.Linear(in_features=in_features, out_features=
out_features, bias=bias)
return OnnxGeneralLinearNew(in_features, out_features, bias, trans_a)
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]
|
ENOT-AutoDL/onnx2torch
|
OnnxGeneralLinear
| false
| 13,632
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
OnnxSoftmaxV1V11
|
import torch
from torch import nn
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxSoftmaxV1V11(nn.Module, OnnxToTorchModule):
def __init__(self, axis: 'int'=1, is_log: 'bool'=False):
super().__init__()
self.axis = axis
self.is_log = is_log
def forward(self, input_tensor: 'torch.Tensor') ->torch.Tensor:
shape = input_tensor.shape
result = torch.flatten(input_tensor, start_dim=self.axis)
result = torch.log_softmax(result, -1
) if self.is_log else torch.softmax(result, -1)
return torch.reshape(result, shape)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import 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_0(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
tl.constexpr):
xnumel = 4
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, float('-inf'))
tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
tmp5 = tmp0 - tmp4
tmp6 = tl_math.exp(tmp5)
tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
tmp9 = tl.where(xmask, tmp7, 0)
tmp10 = tl.sum(tmp9, 1)[:, None]
tmp11 = tmp6 / tmp10
tl.store(out_ptr2 + (r1 + 64 * x0), tmp11, 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)
buf2 = empty_strided_cuda((4, 64), (64, 1), torch.float32)
get_raw_stream(0)
triton_per_fused__softmax_0[grid(4)](arg0_1, buf2, 4, 64, XBLOCK=1,
num_warps=2, num_stages=1)
del arg0_1
return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0),
class OnnxToTorchModule:
"""
Marker class for onnx2torch modules.
"""
pass
class OnnxSoftmaxV1V11New(nn.Module, OnnxToTorchModule):
def __init__(self, axis: 'int'=1, is_log: 'bool'=False):
super().__init__()
self.axis = axis
self.is_log = is_log
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
ENOT-AutoDL/onnx2torch
|
OnnxSoftmaxV1V11
| false
| 13,633
|
[
"Apache-2.0"
] | 144
|
2391987b3349bed1670ac3c1bc9062a37323abe3
|
https://github.com/ENOT-AutoDL/onnx2torch/tree/2391987b3349bed1670ac3c1bc9062a37323abe3
|
SAGEAggregator
|
import torch
import torch.nn as nn
class SAGEAggregator(nn.Module):
def __init__(self, in_features, out_features, agg_method='mean', concat
=False, bias=False):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.concat = concat
self.agg_method = agg_method
self.aggregator = {'mean': torch.mean, 'sum': torch.sum}[agg_method]
self.lin_l = nn.Linear(in_features, out_features, bias=bias)
self.lin_r = nn.Linear(in_features, out_features, bias=bias)
def reset_parameters(self):
self.lin_l.reset_parameters()
self.lin_r.reset_parameters()
def forward(self, x, neigh_x):
if not isinstance(x, torch.Tensor):
x = torch.cat(x, dim=0)
if not isinstance(neigh_x, torch.Tensor):
neigh_x = torch.cat([self.aggregator(h, dim=1) for h in neigh_x
], dim=0)
else:
neigh_x = self.aggregator(neigh_x, dim=1)
neigh_x = self.lin_r(neigh_x)
x = self.lin_l(x)
out = torch.cat([x, neigh_x], dim=1) if self.concat else x + neigh_x
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 16
x1 = xindex // 16
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp6 = tmp4 + tmp5
tmp7 = 4.0
tmp8 = tmp6 / tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
@triton.jit
def triton_poi_fused_add_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 64
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_mean_0[grid(64)](primals_2, buf0, 64, XBLOCK=64,
num_warps=1, num_stages=1)
del primals_2
buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1)
del primals_3
buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2)
del primals_4
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf2
triton_poi_fused_add_1[grid(256)](buf3, buf1, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del buf1
return buf3, reinterpret_tensor(buf0, (16, 4), (4, 1), 0
), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0)
class SAGEAggregatorNew(nn.Module):
def __init__(self, in_features, out_features, agg_method='mean', concat
=False, bias=False):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.concat = concat
self.agg_method = agg_method
self.aggregator = {'mean': torch.mean, 'sum': torch.sum}[agg_method]
self.lin_l = nn.Linear(in_features, out_features, bias=bias)
self.lin_r = nn.Linear(in_features, out_features, bias=bias)
def reset_parameters(self):
self.lin_l.reset_parameters()
self.lin_r.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def forward(self, input_0, input_1):
primals_3 = self.lin_l.weight
primals_4 = self.lin_r.weight
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
EdisonLeeeee/GraphGallery
|
SAGEAggregator
| false
| 13,634
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
HingeLoss
|
import torch
import torch.nn as nn
import torch.utils.data
class HingeLoss(nn.Module):
def __init__(self):
super(HingeLoss, self).__init__()
self.margin = 1.0
def hinge_loss(self, input, target):
output = self.margin - input.mul(target)
output[output.le(0)] = 0
return output.mean()
def forward(self, input, target):
return self.hinge_loss(input, target)
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
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_index_put_lift_fresh_mean_mul_rsub_0(in_out_ptr0,
in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = tl.load(in_ptr1 + r0, None)
tmp2 = tmp0 * tmp1
tmp3 = 1.0
tmp4 = tmp3 - tmp2
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tmp7 = tl.where(tmp6, tmp5, tmp4)
tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
tmp11 = 256.0
tmp12 = tmp10 / tmp11
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp12, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf1 = empty_strided_cuda((), (), torch.float32)
buf2 = buf1
del buf1
get_raw_stream(0)
triton_per_fused_index_put_lift_fresh_mean_mul_rsub_0[grid(1)](buf2,
arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf2,
class HingeLossNew(nn.Module):
def __init__(self):
super(HingeLossNew, self).__init__()
self.margin = 1.0
def hinge_loss(self, input, target):
output = self.margin - input.mul(target)
output[output.le(0)] = 0
return output.mean()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
Enderdead/BinaryConnect_PyTorch
|
HingeLoss
| false
| 13,635
|
[
"MIT"
] | 75
|
990e970b1fbd299ff88200db21a9cc3fe44706d3
|
https://github.com/Enderdead/BinaryConnect_PyTorch/tree/990e970b1fbd299ff88200db21a9cc3fe44706d3
|
GaussionConvD
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class GaussionConvD(nn.Module):
"""The subsequent layer in `RobustGCN` that takes node distribution (mean, var) as input"""
def __init__(self, in_features, out_features, bias=False, gamma=1.0):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w_mean = nn.Linear(in_features, out_features, bias=bias)
self.w_var = nn.Linear(in_features, out_features, bias=bias)
self.gamma = gamma
def reset_parameters(self):
self.w.reset_parameters()
def forward(self, mean, var, adj_mean, adj_var):
mean = F.elu(self.w_mean(mean))
var = F.relu(self.w_var(var))
attention = torch.exp(-self.gamma * var)
mean = adj_mean.mm(mean * attention)
var = adj_var.mm(var * attention * attention)
return mean, var
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4]),
torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, 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_elu_exp_mul_relu_0(in_ptr0, in_ptr1, out_ptr0,
out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp8 = tl.load(in_ptr1 + x0, xmask)
tmp1 = 0.0
tmp2 = tmp0 > tmp1
tmp3 = 1.0
tmp4 = tmp0 * tmp3
tmp5 = libdevice.expm1(tmp4)
tmp6 = tmp5 * tmp3
tmp7 = tl.where(tmp2, tmp4, tmp6)
tmp9 = tl.full([1], 0, tl.int32)
tmp10 = triton_helpers.maximum(tmp9, tmp8)
tmp11 = -1.0
tmp12 = tmp10 * tmp11
tmp13 = tl_math.exp(tmp12)
tmp14 = tmp7 * tmp13
tmp15 = tmp10 * tmp13
tmp16 = tmp15 * tmp13
tl.store(out_ptr0 + x0, tmp14, xmask)
tl.store(out_ptr1 + x0, tmp15, xmask)
tl.store(out_ptr2 + x0, tmp16, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
assert_size_stride(primals_4, (4, 4), (4, 1))
assert_size_stride(primals_5, (4, 4), (4, 1))
assert_size_stride(primals_6, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4),
(1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_4, reinterpret_tensor(primals_3, (4, 4),
(1, 4), 0), out=buf1)
del primals_3
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_elu_exp_mul_relu_0[grid(16)](buf0, buf1, buf2,
buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_5, buf2, out=buf3)
buf6 = buf2
del buf2
extern_kernels.mm(primals_6, buf5, out=buf6)
del buf5
return (buf3, buf6, primals_2, primals_4, buf0, buf1, buf4,
reinterpret_tensor(primals_6, (4, 4), (1, 4), 0),
reinterpret_tensor(primals_5, (4, 4), (1, 4), 0))
class GaussionConvDNew(nn.Module):
"""The subsequent layer in `RobustGCN` that takes node distribution (mean, var) as input"""
def __init__(self, in_features, out_features, bias=False, gamma=1.0):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w_mean = nn.Linear(in_features, out_features, bias=bias)
self.w_var = nn.Linear(in_features, out_features, bias=bias)
self.gamma = gamma
def reset_parameters(self):
self.w.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def forward(self, input_0, input_1, input_2, input_3):
primals_1 = self.w_mean.weight
primals_2 = self.w_var.weight
primals_3 = input_0
primals_4 = input_1
primals_5 = input_2
primals_6 = input_3
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6])
return output[0], output[1]
|
EdisonLeeeee/GraphGallery
|
GaussionConvD
| false
| 13,636
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
APPNProp
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class SparseDropout(nn.Module):
def __init__(self, p=0.5):
super().__init__()
self.p = p
def forward(self, x):
x_coal = x.coalesce()
drop_val = F.dropout(x_coal._values(), self.p, self.training)
return torch.sparse.FloatTensor(x_coal._indices(), drop_val, x.shape)
class MixedDropout(nn.Module):
def __init__(self, p=0.5):
super().__init__()
self.dense_dropout = nn.Dropout(p)
self.sparse_dropout = SparseDropout(p)
def forward(self, x):
if x.is_sparse:
return self.sparse_dropout(x)
else:
return self.dense_dropout(x)
class APPNProp(nn.Module):
def __init__(self, alpha: 'float'=0.1, K: 'int'=10, dropout: 'float'=0.0):
super().__init__()
self.alpha = alpha
self.K = K
if not dropout:
self.dropout = lambda x: x
else:
self.dropout = MixedDropout(dropout)
def forward(self, x, adj):
h = x
for _ in range(self.K):
A_drop = self.dropout(adj)
h = (1 - self.alpha) * A_drop.mm(h) + self.alpha * x
return h
def __repr__(self):
return (
f'{self.__class__.__name__}(alpha={self.alpha}, K={self.K}, dropout={self.dropout})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_out_ptr0 + x0, xmask)
tmp3 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.9
tmp2 = tmp0 * tmp1
tmp4 = 0.1
tmp5 = tmp3 * tmp4
tmp6 = tmp2 + tmp5
tl.store(in_out_ptr0 + x0, tmp6, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4), (4, 1))
assert_size_stride(arg1_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, arg0_1, out=buf0)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_add_mul_0[grid(16)](buf1, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf1, out=buf2)
buf3 = buf2
del buf2
triton_poi_fused_add_mul_0[grid(16)](buf3, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf4 = buf1
del buf1
extern_kernels.mm(arg1_1, buf3, out=buf4)
buf5 = buf4
del buf4
triton_poi_fused_add_mul_0[grid(16)](buf5, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf6 = buf3
del buf3
extern_kernels.mm(arg1_1, buf5, out=buf6)
buf7 = buf6
del buf6
triton_poi_fused_add_mul_0[grid(16)](buf7, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf8 = buf5
del buf5
extern_kernels.mm(arg1_1, buf7, out=buf8)
buf9 = buf8
del buf8
triton_poi_fused_add_mul_0[grid(16)](buf9, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf10 = buf7
del buf7
extern_kernels.mm(arg1_1, buf9, out=buf10)
buf11 = buf10
del buf10
triton_poi_fused_add_mul_0[grid(16)](buf11, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf12 = buf9
del buf9
extern_kernels.mm(arg1_1, buf11, out=buf12)
buf13 = buf12
del buf12
triton_poi_fused_add_mul_0[grid(16)](buf13, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf14 = buf11
del buf11
extern_kernels.mm(arg1_1, buf13, out=buf14)
buf15 = buf14
del buf14
triton_poi_fused_add_mul_0[grid(16)](buf15, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf16 = buf13
del buf13
extern_kernels.mm(arg1_1, buf15, out=buf16)
buf17 = buf16
del buf16
triton_poi_fused_add_mul_0[grid(16)](buf17, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf18 = buf15
del buf15
extern_kernels.mm(arg1_1, buf17, out=buf18)
del arg1_1
del buf17
buf19 = buf18
del buf18
triton_poi_fused_add_mul_0[grid(16)](buf19, arg0_1, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del arg0_1
return buf19,
class SparseDropout(nn.Module):
def __init__(self, p=0.5):
super().__init__()
self.p = p
def forward(self, x):
x_coal = x.coalesce()
drop_val = F.dropout(x_coal._values(), self.p, self.training)
return torch.sparse.FloatTensor(x_coal._indices(), drop_val, x.shape)
class MixedDropout(nn.Module):
def __init__(self, p=0.5):
super().__init__()
self.dense_dropout = nn.Dropout(p)
self.sparse_dropout = SparseDropout(p)
def forward(self, x):
if x.is_sparse:
return self.sparse_dropout(x)
else:
return self.dense_dropout(x)
class APPNPropNew(nn.Module):
def __init__(self, alpha: 'float'=0.1, K: 'int'=10, dropout: 'float'=0.0):
super().__init__()
self.alpha = alpha
self.K = K
if not dropout:
self.dropout = lambda x: x
else:
self.dropout = MixedDropout(dropout)
def __repr__(self):
return (
f'{self.__class__.__name__}(alpha={self.alpha}, K={self.K}, dropout={self.dropout})'
)
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EdisonLeeeee/GraphGallery
|
APPNProp
| false
| 13,637
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
CumulativeLinkLoss
|
import torch
import numpy as np
from torch import nn
from typing import Optional
def _reduction(loss: 'torch.Tensor', reduction: 'str') ->torch.Tensor:
"""
Reduce loss
Parameters
----------
loss : torch.Tensor, [batch_size, num_classes]
Batch losses.
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
Returns
-------
loss : torch.Tensor
Reduced loss.
"""
if reduction == 'elementwise_mean':
return loss.mean()
elif reduction == 'none':
return loss
elif reduction == 'sum':
return loss.sum()
else:
raise ValueError(f'{reduction} is not a valid reduction')
def cumulative_link_loss(y_pred: 'torch.Tensor', y_true: 'torch.Tensor',
reduction: 'str'='elementwise_mean', class_weights:
'Optional[np.ndarray]'=None) ->torch.Tensor:
"""
Calculates the negative log likelihood using the logistic cumulative link
function.
See "On the consistency of ordinal regression methods", Pedregosa et. al.
for more details. While this paper is not the first to introduce this, it
is the only one that I could find that was easily readable outside of
paywalls.
Parameters
----------
y_pred : torch.Tensor, [batch_size, num_classes]
Predicted target class probabilities. float dtype.
y_true : torch.Tensor, [batch_size, 1]
True target classes. long dtype.
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
class_weights : np.ndarray, [num_classes] optional (default=None)
An array of weights for each class. If included, then for each sample,
look up the true class and multiply that sample's loss by the weight in
this array.
Returns
-------
loss: torch.Tensor
"""
eps = 1e-15
likelihoods = torch.clamp(torch.gather(y_pred, 1, y_true), eps, 1 - eps)
neg_log_likelihood = -torch.log(likelihoods)
if class_weights is not None:
class_weights = torch.as_tensor(class_weights, dtype=
neg_log_likelihood.dtype, device=neg_log_likelihood.device)
neg_log_likelihood *= class_weights[y_true]
loss = _reduction(neg_log_likelihood, reduction)
return loss
class CumulativeLinkLoss(nn.Module):
"""
Module form of cumulative_link_loss() loss function
Parameters
----------
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
class_weights : np.ndarray, [num_classes] optional (default=None)
An array of weights for each class. If included, then for each sample,
look up the true class and multiply that sample's loss by the weight in
this array.
"""
def __init__(self, reduction: 'str'='elementwise_mean', class_weights:
'Optional[torch.Tensor]'=None) ->None:
super().__init__()
self.class_weights = class_weights
self.reduction = reduction
def forward(self, y_pred: 'torch.Tensor', y_true: 'torch.Tensor'
) ->torch.Tensor:
return cumulative_link_loss(y_pred, y_true, reduction=self.
reduction, class_weights=self.class_weights)
def get_inputs():
return [torch.ones([4, 4], dtype=torch.int64), torch.ones([4, 4], dtype
=torch.int64)]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import numpy as np
from torch import nn
from typing import Optional
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_clamp_gather_log_mean_neg_0(in_out_ptr0, in_ptr0,
in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
r1 = rindex // 4
tmp0 = tl.load(in_ptr0 + r2, None)
tmp1 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
tmp2 = tmp0 + tmp1
tmp3 = tmp0 < 0
tmp4 = tl.where(tmp3, tmp2, tmp0)
tl.device_assert((0 <= tmp4) & (tmp4 < 4),
'index out of bounds: 0 <= tmp4 < 4')
tmp6 = tl.load(in_ptr1 + (tmp4 + 4 * r1), None, eviction_policy=
'evict_last')
tmp7 = tmp6.to(tl.float32)
tmp8 = 1e-15
tmp9 = triton_helpers.maximum(tmp7, tmp8)
tmp10 = 0.999999999999999
tmp11 = triton_helpers.minimum(tmp9, tmp10)
tmp12 = tmp11.to(tl.int64)
tmp13 = tmp12.to(tl.float32)
tmp14 = tl_math.log(tmp13)
tmp15 = -tmp14
tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
tmp18 = tl.sum(tmp16, 1)[:, None]
tmp19 = 16.0
tmp20 = tmp18 / tmp19
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp20, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4), (4, 1))
assert_size_stride(arg1_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_clamp_gather_log_mean_neg_0[grid(1)](buf1, arg1_1,
arg0_1, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf1,
def _reduction(loss: 'torch.Tensor', reduction: 'str') ->torch.Tensor:
"""
Reduce loss
Parameters
----------
loss : torch.Tensor, [batch_size, num_classes]
Batch losses.
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
Returns
-------
loss : torch.Tensor
Reduced loss.
"""
if reduction == 'elementwise_mean':
return loss.mean()
elif reduction == 'none':
return loss
elif reduction == 'sum':
return loss.sum()
else:
raise ValueError(f'{reduction} is not a valid reduction')
def cumulative_link_loss(y_pred: 'torch.Tensor', y_true: 'torch.Tensor',
reduction: 'str'='elementwise_mean', class_weights:
'Optional[np.ndarray]'=None) ->torch.Tensor:
"""
Calculates the negative log likelihood using the logistic cumulative link
function.
See "On the consistency of ordinal regression methods", Pedregosa et. al.
for more details. While this paper is not the first to introduce this, it
is the only one that I could find that was easily readable outside of
paywalls.
Parameters
----------
y_pred : torch.Tensor, [batch_size, num_classes]
Predicted target class probabilities. float dtype.
y_true : torch.Tensor, [batch_size, 1]
True target classes. long dtype.
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
class_weights : np.ndarray, [num_classes] optional (default=None)
An array of weights for each class. If included, then for each sample,
look up the true class and multiply that sample's loss by the weight in
this array.
Returns
-------
loss: torch.Tensor
"""
eps = 1e-15
likelihoods = torch.clamp(torch.gather(y_pred, 1, y_true), eps, 1 - eps)
neg_log_likelihood = -torch.log(likelihoods)
if class_weights is not None:
class_weights = torch.as_tensor(class_weights, dtype=
neg_log_likelihood.dtype, device=neg_log_likelihood.device)
neg_log_likelihood *= class_weights[y_true]
loss = _reduction(neg_log_likelihood, reduction)
return loss
class CumulativeLinkLossNew(nn.Module):
"""
Module form of cumulative_link_loss() loss function
Parameters
----------
reduction : str
Method for reducing the loss. Options include 'elementwise_mean',
'none', and 'sum'.
class_weights : np.ndarray, [num_classes] optional (default=None)
An array of weights for each class. If included, then for each sample,
look up the true class and multiply that sample's loss by the weight in
this array.
"""
def __init__(self, reduction: 'str'='elementwise_mean', class_weights:
'Optional[torch.Tensor]'=None) ->None:
super().__init__()
self.class_weights = class_weights
self.reduction = reduction
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EthanRosenthal/spacecutter
|
CumulativeLinkLoss
| false
| 13,638
|
[
"MIT"
] | 74
|
37a6f7367905b50e7886dc1ef2bfe1d63220347a
|
https://github.com/EthanRosenthal/spacecutter/tree/37a6f7367905b50e7886dc1ef2bfe1d63220347a
|
SinkhornKnopp
|
import torch
class SinkhornKnopp(torch.nn.Module):
def __init__(self, num_iters=3, epsilon=0.05):
super().__init__()
self.num_iters = num_iters
self.epsilon = epsilon
@torch.no_grad()
def forward(self, logits):
Q = torch.exp(logits / self.epsilon).t()
B = Q.shape[1]
K = Q.shape[0]
sum_Q = torch.sum(Q)
Q /= sum_Q
for it in range(self.num_iters):
sum_of_rows = torch.sum(Q, dim=1, keepdim=True)
Q /= sum_of_rows
Q /= K
Q /= torch.sum(Q, dim=0, keepdim=True)
Q /= B
Q *= B
return Q.t()
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
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_per_fused_sum_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
constexpr):
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp1 = 20.0
tmp2 = tmp0 * tmp1
tmp3 = tl_math.exp(tmp2)
tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tmp6 = tl.sum(tmp4, 1)[:, None]
tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None)
@triton.jit
def triton_poi_fused_sum_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp4 = tl.load(in_ptr1 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp7 = tl.load(in_ptr0 + (4 + x0), xmask)
tmp12 = tl.load(in_ptr0 + (8 + x0), xmask)
tmp17 = tl.load(in_ptr0 + (12 + x0), xmask)
tmp1 = 20.0
tmp2 = tmp0 * tmp1
tmp3 = tl_math.exp(tmp2)
tmp6 = tmp3 / tmp5
tmp8 = tmp7 * tmp1
tmp9 = tl_math.exp(tmp8)
tmp10 = tmp9 / tmp5
tmp11 = tmp6 + tmp10
tmp13 = tmp12 * tmp1
tmp14 = tl_math.exp(tmp13)
tmp15 = tmp14 / tmp5
tmp16 = tmp11 + tmp15
tmp18 = tmp17 * tmp1
tmp19 = tl_math.exp(tmp18)
tmp20 = tmp19 / tmp5
tmp21 = tmp16 + tmp20
tl.store(out_ptr0 + x0, tmp21, xmask)
@triton.jit
def triton_poi_fused_sum_2(in_ptr0, in_ptr1, in_ptr2, 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')
tmp4 = tl.load(in_ptr1 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp7 = tl.load(in_ptr2 + 0)
tmp8 = tl.broadcast_to(tmp7, [XBLOCK])
tmp12 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp16 = tl.load(in_ptr2 + 1)
tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
tmp21 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp25 = tl.load(in_ptr2 + 2)
tmp26 = tl.broadcast_to(tmp25, [XBLOCK])
tmp30 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
)
tmp34 = tl.load(in_ptr2 + 3)
tmp35 = tl.broadcast_to(tmp34, [XBLOCK])
tmp1 = 20.0
tmp2 = tmp0 * tmp1
tmp3 = tl_math.exp(tmp2)
tmp6 = tmp3 / tmp5
tmp9 = tmp6 / tmp8
tmp10 = 0.25
tmp11 = tmp9 * tmp10
tmp13 = tmp12 * tmp1
tmp14 = tl_math.exp(tmp13)
tmp15 = tmp14 / tmp5
tmp18 = tmp15 / tmp17
tmp19 = tmp18 * tmp10
tmp20 = tmp11 + tmp19
tmp22 = tmp21 * tmp1
tmp23 = tl_math.exp(tmp22)
tmp24 = tmp23 / tmp5
tmp27 = tmp24 / tmp26
tmp28 = tmp27 * tmp10
tmp29 = tmp20 + tmp28
tmp31 = tmp30 * tmp1
tmp32 = tl_math.exp(tmp31)
tmp33 = tmp32 / tmp5
tmp36 = tmp33 / tmp35
tmp37 = tmp36 * tmp10
tmp38 = tmp29 + tmp37
tl.store(out_ptr0 + x0, tmp38, xmask)
@triton.jit
def triton_poi_fused_sum_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 4
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0, xmask)
tmp4 = tl.load(in_ptr1 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp7 = tl.load(in_ptr2 + x0, xmask)
tmp11 = tl.load(in_ptr3 + 0)
tmp12 = tl.broadcast_to(tmp11, [XBLOCK])
tmp15 = tl.load(in_ptr0 + (4 + x0), xmask)
tmp21 = tl.load(in_ptr3 + 1)
tmp22 = tl.broadcast_to(tmp21, [XBLOCK])
tmp26 = tl.load(in_ptr0 + (8 + x0), xmask)
tmp32 = tl.load(in_ptr3 + 2)
tmp33 = tl.broadcast_to(tmp32, [XBLOCK])
tmp37 = tl.load(in_ptr0 + (12 + x0), xmask)
tmp43 = tl.load(in_ptr3 + 3)
tmp44 = tl.broadcast_to(tmp43, [XBLOCK])
tmp1 = 20.0
tmp2 = tmp0 * tmp1
tmp3 = tl_math.exp(tmp2)
tmp6 = tmp3 / tmp5
tmp8 = tmp6 / tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tmp13 = tmp10 / tmp12
tmp14 = tmp13 * tmp9
tmp16 = tmp15 * tmp1
tmp17 = tl_math.exp(tmp16)
tmp18 = tmp17 / tmp5
tmp19 = tmp18 / tmp7
tmp20 = tmp19 * tmp9
tmp23 = tmp20 / tmp22
tmp24 = tmp23 * tmp9
tmp25 = tmp14 + tmp24
tmp27 = tmp26 * tmp1
tmp28 = tl_math.exp(tmp27)
tmp29 = tmp28 / tmp5
tmp30 = tmp29 / tmp7
tmp31 = tmp30 * tmp9
tmp34 = tmp31 / tmp33
tmp35 = tmp34 * tmp9
tmp36 = tmp25 + tmp35
tmp38 = tmp37 * tmp1
tmp39 = tl_math.exp(tmp38)
tmp40 = tmp39 / tmp5
tmp41 = tmp40 / tmp7
tmp42 = tmp41 * tmp9
tmp45 = tmp42 / tmp44
tmp46 = tmp45 * tmp9
tmp47 = tmp36 + tmp46
tl.store(out_ptr0 + x0, tmp47, xmask)
@triton.jit
def triton_poi_fused_div_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp4 = tl.load(in_ptr1 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp7 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
tmp11 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
tmp14 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
tmp1 = 20.0
tmp2 = tmp0 * tmp1
tmp3 = tl_math.exp(tmp2)
tmp6 = tmp3 / tmp5
tmp8 = tmp6 / tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tmp12 = tmp10 / tmp11
tmp13 = tmp12 * tmp9
tmp15 = tmp13 / tmp14
tmp16 = tmp15 * tmp9
tl.store(out_ptr0 + x2, tmp16, xmask)
@triton.jit
def triton_poi_fused_div_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 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tl.store(out_ptr0 + x2, tmp10, xmask)
@triton.jit
def triton_poi_fused_div_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
x0 = xindex % 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tl.store(out_ptr0 + x2, tmp10, xmask)
@triton.jit
def triton_poi_fused_mul_7(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 4
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x1 = xindex
y0 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp3 = tmp1 + tmp2
tmp5 = tmp3 + tmp4
tmp7 = tmp5 + tmp6
tmp8 = tmp0 / tmp7
tmp9 = 0.25
tmp10 = tmp8 * tmp9
tmp11 = 4.0
tmp12 = tmp10 * tmp11
tl.store(out_ptr0 + (x1 + 4 * y0), tmp12, xmask & ymask)
@triton.jit
def triton_poi_fused_8(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
constexpr, XBLOCK: tl.constexpr):
ynumel = 4
xnumel = 4
yoffset = tl.program_id(1) * YBLOCK
yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
ymask = yindex < ynumel
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
x1 = xindex
y0 = yindex
tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask)
def call(args):
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((), (), torch.float32)
get_raw_stream(0)
triton_per_fused_sum_0[grid(1)](arg0_1, buf0, 1, 16, XBLOCK=1,
num_warps=2, num_stages=1)
buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
triton_poi_fused_sum_1[grid(4)](arg0_1, buf0, buf1, 4, XBLOCK=4,
num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
triton_poi_fused_sum_2[grid(4)](arg0_1, buf0, buf1, buf2, 4, XBLOCK
=4, num_warps=1, num_stages=1)
buf3 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
triton_poi_fused_sum_3[grid(4)](arg0_1, buf0, buf1, buf2, buf3, 4,
XBLOCK=4, num_warps=1, num_stages=1)
buf4 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
triton_poi_fused_div_4[grid(16)](arg0_1, buf0, buf1, buf2, buf3,
buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
del arg0_1
del buf0
del buf1
del buf2
del buf3
buf5 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
triton_poi_fused_div_5[grid(16)](buf4, buf5, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf6 = buf4
del buf4
triton_poi_fused_div_6[grid(16)](buf5, buf6, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf7 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0)
del buf5
triton_poi_fused_mul_7[grid(4, 4)](buf6, buf7, 4, 4, XBLOCK=4,
YBLOCK=4, num_warps=1, num_stages=1)
buf8 = reinterpret_tensor(buf6, (4, 4), (4, 1), 0)
del buf6
triton_poi_fused_8[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4, YBLOCK=4,
num_warps=1, num_stages=1)
del buf7
return buf8,
class SinkhornKnoppNew(torch.nn.Module):
def __init__(self, num_iters=3, epsilon=0.05):
super().__init__()
self.num_iters = num_iters
self.epsilon = epsilon
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
DonkeyShot21/UNO
|
SinkhornKnopp
| false
| 13,639
|
[
"MIT"
] | 87
|
7613d3f8c58e5f16ee0d68fdd803ef442d819af4
|
https://github.com/DonkeyShot21/UNO/tree/7613d3f8c58e5f16ee0d68fdd803ef442d819af4
|
DAGNNConv
|
import torch
import torch.nn as nn
class DAGNNConv(nn.Module):
def __init__(self, in_features, out_features=1, K=10, bias=False):
super().__init__()
assert out_features == 1, "'out_features' must be 1"
self.in_features = in_features
self.out_features = out_features
self.lin = nn.Linear(in_features, out_features, bias=bias)
self.K = K
self.act = nn.Sigmoid()
def reset_parameters(self):
self.lin.reset_parameters()
def forward(self, x, adj):
propagations = [x]
for _ in range(self.K):
x = adj.mm(x)
propagations.append(x)
h = torch.stack(propagations, dim=1)
retain_score = self.act(self.lin(h)).permute(0, 2, 1).contiguous()
out = (retain_score @ h).squeeze(1)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_stack_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tl.store(out_ptr0 + (x0 + 44 * x1), tmp0, xmask)
@triton.jit
def triton_poi_fused_stack_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 + 44 * x1), tmp0, xmask)
@triton.jit
def triton_poi_fused_sigmoid_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 44
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.sigmoid(tmp0)
tl.store(out_ptr0 + x0, tmp1, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (1, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, primals_2, out=buf0)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf0, out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf1, out=buf2)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf2, out=buf3)
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf3, out=buf4)
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf4, out=buf5)
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf5, out=buf6)
buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf6, out=buf7)
buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_1, buf7, out=buf8)
buf20 = empty_strided_cuda((4, 44), (44, 1), torch.float32)
buf9 = reinterpret_tensor(buf20, (4, 4), (44, 1), 40)
extern_kernels.mm(primals_1, buf8, out=buf9)
del primals_1
buf10 = reinterpret_tensor(buf20, (4, 4), (44, 1), 0)
get_raw_stream(0)
triton_poi_fused_stack_0[grid(16)](primals_2, buf10, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_2
buf11 = reinterpret_tensor(buf20, (4, 4), (44, 1), 4)
triton_poi_fused_stack_1[grid(16)](buf0, buf11, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf0
buf12 = reinterpret_tensor(buf20, (4, 4), (44, 1), 8)
triton_poi_fused_stack_1[grid(16)](buf1, buf12, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf1
buf13 = reinterpret_tensor(buf20, (4, 4), (44, 1), 12)
triton_poi_fused_stack_1[grid(16)](buf2, buf13, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf2
buf14 = reinterpret_tensor(buf20, (4, 4), (44, 1), 16)
triton_poi_fused_stack_0[grid(16)](buf3, buf14, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf3
buf15 = reinterpret_tensor(buf20, (4, 4), (44, 1), 20)
triton_poi_fused_stack_1[grid(16)](buf4, buf15, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf4
buf16 = reinterpret_tensor(buf20, (4, 4), (44, 1), 24)
triton_poi_fused_stack_1[grid(16)](buf5, buf16, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf5
buf17 = reinterpret_tensor(buf20, (4, 4), (44, 1), 28)
triton_poi_fused_stack_1[grid(16)](buf6, buf17, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf6
buf18 = reinterpret_tensor(buf20, (4, 4), (44, 1), 32)
triton_poi_fused_stack_0[grid(16)](buf7, buf18, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf7
buf19 = reinterpret_tensor(buf20, (4, 4), (44, 1), 36)
triton_poi_fused_stack_1[grid(16)](buf8, buf19, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf21 = empty_strided_cuda((44, 1), (1, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(buf20, (44, 4), (4, 1), 0),
reinterpret_tensor(primals_3, (4, 1), (1, 4), 0), out=buf21)
del primals_3
buf22 = empty_strided_cuda((4, 11, 1), (11, 1, 1), torch.float32)
triton_poi_fused_sigmoid_2[grid(44)](buf21, buf22, 44, XBLOCK=64,
num_warps=1, num_stages=1)
buf23 = reinterpret_tensor(buf8, (4, 1, 4), (4, 4, 1), 0)
del buf8
extern_kernels.bmm(reinterpret_tensor(buf22, (4, 1, 11), (11, 0, 1),
0), reinterpret_tensor(buf20, (4, 11, 4), (44, 4, 1), 0), out=buf23
)
del buf22
return reinterpret_tensor(buf23, (4, 4), (4, 1), 0), buf20, buf21
class DAGNNConvNew(nn.Module):
def __init__(self, in_features, out_features=1, K=10, bias=False):
super().__init__()
assert out_features == 1, "'out_features' must be 1"
self.in_features = in_features
self.out_features = out_features
self.lin = nn.Linear(in_features, out_features, bias=bias)
self.K = K
self.act = nn.Sigmoid()
def reset_parameters(self):
self.lin.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})'
)
def forward(self, input_0, input_1):
primals_3 = self.lin.weight
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3])
return output[0]
|
EdisonLeeeee/GraphGallery
|
DAGNNConv
| false
| 13,640
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
TransitionUp
|
import torch
from torch import nn
import torch.utils.data
def center_crop(layer, max_height, max_width):
_, _, h, w = layer.size()
xy1 = (w - max_width) // 2
xy2 = (h - max_height) // 2
return layer[:, :, xy2:xy2 + max_height, xy1:xy1 + max_width]
class TransitionUp(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.convTrans = nn.ConvTranspose2d(in_channels=in_channels,
out_channels=out_channels, kernel_size=3, stride=2, padding=0,
bias=True)
def forward(self, x, skip):
out = self.convTrans(x)
out = center_crop(out, skip.size(2), skip.size(3))
out = torch.cat([out, skip], 1)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channels': 4, 'out_channels': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch import nn
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, 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
x2 = xindex // 16 % 8
x0 = xindex % 4
x1 = xindex // 4 % 4
x3 = xindex // 128
x4 = xindex % 16
x5 = xindex
tmp0 = x2
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + (20 + x0 + 9 * x1 + 81 * x2 + 324 * x3), tmp4 &
xmask, other=0.0)
tmp6 = tl.load(in_ptr1 + x2, 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], 8, tl.int64)
tmp13 = tl.load(in_ptr2 + (x4 + 16 * (-4 + x2) + 64 * x3), tmp10 &
xmask, other=0.0)
tmp14 = tl.where(tmp4, tmp9, tmp13)
tl.store(out_ptr0 + x5, tmp14, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_2, (4,), (1,))
assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(2,
2), padding=(0, 0), dilation=(1, 1), transposed=True,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf0, (4, 4, 9, 9), (324, 81, 9, 1))
buf1 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(512)](buf0, primals_2, primals_4, buf1,
512, XBLOCK=256, num_warps=4, num_stages=1)
del buf0
del primals_2
del primals_4
return buf1, primals_1, primals_3
def center_crop(layer, max_height, max_width):
_, _, h, w = layer.size()
xy1 = (w - max_width) // 2
xy2 = (h - max_height) // 2
return layer[:, :, xy2:xy2 + max_height, xy1:xy1 + max_width]
class TransitionUpNew(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.convTrans = nn.ConvTranspose2d(in_channels=in_channels,
out_channels=out_channels, kernel_size=3, stride=2, padding=0,
bias=True)
def forward(self, input_0, input_1):
primals_1 = self.convTrans.weight
primals_2 = self.convTrans.bias
primals_3 = input_0
primals_4 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
ELEKTRONN/elektronn3
|
TransitionUp
| false
| 13,641
|
[
"MIT"
] | 124
|
19c751855dffc67b744cd43e757aa4a5bd577d9b
|
https://github.com/ELEKTRONN/elektronn3/tree/19c751855dffc67b744cd43e757aa4a5bd577d9b
|
SSGConv
|
from torch.nn import Module
import torch
class SSGConv(Module):
def __init__(self, K=16, alpha=0.1, **kwargs):
super().__init__()
assert K > 0
self.K = K
self.alpha = alpha
def forward(self, x, adj):
x_in = x
x_out = torch.zeros_like(x)
for _ in range(self.K):
x = torch.spmm(adj, x)
x_out += (1 - self.alpha) * x
x_out /= self.K
x_out += self.alpha * x_in
return x_out
def reset_parameters(self):
pass
def extra_repr(self):
return f'K={self.K}, alpha={self.alpha}'
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch.nn import Module
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_add_div_mul_0(in_out_ptr0, in_out_ptr1, in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8,
in_ptr9, in_ptr10, in_ptr11, in_ptr12, in_ptr13, in_ptr14, 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)
tmp3 = tl.load(in_ptr0 + x0, xmask)
tmp6 = tl.load(in_ptr1 + x0, xmask)
tmp9 = tl.load(in_ptr2 + x0, xmask)
tmp12 = tl.load(in_ptr3 + x0, xmask)
tmp15 = tl.load(in_ptr4 + x0, xmask)
tmp18 = tl.load(in_ptr5 + x0, xmask)
tmp21 = tl.load(in_ptr6 + x0, xmask)
tmp24 = tl.load(in_ptr7 + x0, xmask)
tmp27 = tl.load(in_out_ptr1 + x0, xmask)
tmp30 = tl.load(in_ptr8 + x0, xmask)
tmp33 = tl.load(in_ptr9 + x0, xmask)
tmp36 = tl.load(in_ptr10 + x0, xmask)
tmp39 = tl.load(in_ptr11 + x0, xmask)
tmp42 = tl.load(in_ptr12 + x0, xmask)
tmp45 = tl.load(in_ptr13 + x0, xmask)
tmp50 = tl.load(in_ptr14 + x0, xmask)
tmp1 = 0.9
tmp2 = tmp0 * tmp1
tmp4 = tmp3 * tmp1
tmp5 = tmp2 + tmp4
tmp7 = tmp6 * tmp1
tmp8 = tmp5 + tmp7
tmp10 = tmp9 * tmp1
tmp11 = tmp8 + tmp10
tmp13 = tmp12 * tmp1
tmp14 = tmp11 + tmp13
tmp16 = tmp15 * tmp1
tmp17 = tmp14 + tmp16
tmp19 = tmp18 * tmp1
tmp20 = tmp17 + tmp19
tmp22 = tmp21 * tmp1
tmp23 = tmp20 + tmp22
tmp25 = tmp24 * tmp1
tmp26 = tmp23 + tmp25
tmp28 = tmp27 * tmp1
tmp29 = tmp26 + tmp28
tmp31 = tmp30 * tmp1
tmp32 = tmp29 + tmp31
tmp34 = tmp33 * tmp1
tmp35 = tmp32 + tmp34
tmp37 = tmp36 * tmp1
tmp38 = tmp35 + tmp37
tmp40 = tmp39 * tmp1
tmp41 = tmp38 + tmp40
tmp43 = tmp42 * tmp1
tmp44 = tmp41 + tmp43
tmp46 = tmp45 * tmp1
tmp47 = tmp44 + tmp46
tmp48 = 0.0625
tmp49 = tmp47 * tmp48
tmp51 = 0.1
tmp52 = tmp50 * tmp51
tmp53 = tmp49 + tmp52
tl.store(in_out_ptr1 + x0, tmp53, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4), (4, 1))
assert_size_stride(arg1_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, arg0_1, out=buf0)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf0, out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf1, out=buf2)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf2, out=buf3)
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf3, out=buf4)
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf4, out=buf5)
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf5, out=buf6)
buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf6, out=buf7)
buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf7, out=buf8)
buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf8, out=buf10)
buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf10, out=buf11)
buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf11, out=buf12)
buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf12, out=buf13)
buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf13, out=buf14)
buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf14, out=buf15)
buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg1_1, buf15, out=buf16)
del arg1_1
buf9 = buf0
del buf0
buf17 = buf10
del buf10
get_raw_stream(0)
triton_poi_fused_add_div_mul_0[grid(16)](buf9, buf17, buf1, buf2,
buf3, buf4, buf5, buf6, buf7, buf8, buf11, buf12, buf13, buf14,
buf15, buf16, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1)
del arg0_1
del buf1
del buf11
del buf12
del buf13
del buf14
del buf15
del buf16
del buf2
del buf3
del buf4
del buf5
del buf6
del buf7
del buf8
del buf9
return buf17,
class SSGConvNew(Module):
def __init__(self, K=16, alpha=0.1, **kwargs):
super().__init__()
assert K > 0
self.K = K
self.alpha = alpha
def reset_parameters(self):
pass
def extra_repr(self):
return f'K={self.K}, alpha={self.alpha}'
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EdisonLeeeee/GraphGallery
|
SSGConv
| false
| 13,642
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
WaveletConv
|
import torch
import torch.nn as nn
class WaveletConv(nn.Module):
def __init__(self, in_features, out_features, num_nodes, bias=False):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
self.filter = nn.Parameter(torch.ones(num_nodes, 1))
def reset_parameters(self):
self.w.reset_parameters()
self.filter.data.fill_(1.0)
def forward(self, x, wavelet, inverse_wavelet):
h = self.w(x)
h = inverse_wavelet.mm(h)
h = self.filter * h
out = wavelet.mm(h)
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4, 'num_nodes': 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_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_ptr1 + x2, xmask)
tmp2 = tmp0 * tmp1
tl.store(out_ptr0 + x2, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 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, 1), (1, 1))
assert_size_stride(primals_5, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4),
(1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf0, out=buf1)
buf2 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_mul_0[grid(16)](primals_4, buf1, buf2, 16, XBLOCK=
16, num_warps=1, num_stages=1)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_5, buf2, out=buf3)
del buf2
return buf3, primals_2, primals_4, buf1, reinterpret_tensor(primals_5,
(4, 4), (1, 4), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0)
class WaveletConvNew(nn.Module):
def __init__(self, in_features, out_features, num_nodes, bias=False):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
self.filter = nn.Parameter(torch.ones(num_nodes, 1))
def reset_parameters(self):
self.w.reset_parameters()
self.filter.data.fill_(1.0)
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features})'
)
def forward(self, input_0, input_1, input_2):
primals_4 = self.filter
primals_1 = self.w.weight
primals_2 = input_0
primals_3 = input_1
primals_5 = input_2
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
EdisonLeeeee/GraphGallery
|
WaveletConv
| false
| 13,643
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
ResidualBlock
|
import torch
class ConvLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvLayer, self).__init__()
reflection_padding = 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):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ResidualBlock(torch.nn.Module):
"""ResidualBlock
introduced in: https://arxiv.org/abs/1512.03385
recommended architecture: http://torch.ch/blog/2016/02/04/resnets.html
"""
def __init__(self, channels):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in1 = torch.nn.InstanceNorm2d(channels, affine=True)
self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in2 = torch.nn.InstanceNorm2d(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
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
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 576
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 6
x1 = xindex // 6 % 6
x2 = xindex // 36
x3 = xindex
tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 +
x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x2),
xmask, eviction_policy='evict_last')
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_per_fused__native_batch_norm_legit_convolution_1(in_out_ptr0,
in_out_ptr1, 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)
r2 = rindex
x3 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tl.where(xmask, tmp3, 0)
tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tmp8 = tl.where(xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp3 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.where(xmask, tmp15, 0)
tmp18 = tl.sum(tmp17, 1)[:, None]
tmp19 = 16.0
tmp20 = tmp18 / tmp19
tmp21 = 1e-05
tmp22 = tmp20 + tmp21
tmp23 = libdevice.rsqrt(tmp22)
tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask)
tl.debug_barrier()
tl.store(in_out_ptr1 + x3, tmp23, xmask)
tl.store(out_ptr0 + x3, tmp12, xmask)
@triton.jit
def triton_poi_fused_repeat_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + x0 % 4, 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 = 576
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 6
x1 = xindex // 6 % 6
x2 = xindex // 36
x3 = xindex
tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 +
x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x2),
xmask, eviction_policy='evict_last')
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_4(
in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1,
out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK: tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
x0 = xindex
r3 = rindex
x1 = xindex % 4
tmp0 = tl.load(in_ptr0 + x0 % 4, xmask, eviction_policy='evict_last')
tmp1 = tl.load(in_out_ptr0 + (r3 + 16 * x0), xmask, other=0.0)
tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
tmp28 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
tmp30 = tl.load(in_ptr3 + (r3 + 16 * x0), xmask, other=0.0)
tmp3 = tmp1 + tmp2
tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tl.where(xmask, tmp4, 0)
tmp7 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
tmp9 = tl.where(xmask, tmp7, 0)
tmp10 = tl.sum(tmp9, 1)[:, None]
tmp11 = tl.full([XBLOCK, 1], 16, tl.int32)
tmp12 = tmp11.to(tl.float32)
tmp13 = tmp10 / tmp12
tmp14 = tmp4 - tmp13
tmp15 = tmp14 * tmp14
tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
tmp18 = tl.where(xmask, tmp16, 0)
tmp19 = tl.sum(tmp18, 1)[:, None]
tmp20 = tmp3 - tmp13
tmp21 = 16.0
tmp22 = tmp19 / tmp21
tmp23 = 1e-05
tmp24 = tmp22 + tmp23
tmp25 = libdevice.rsqrt(tmp24)
tmp26 = tmp20 * tmp25
tmp27 = tmp26 * tmp0
tmp29 = tmp27 + tmp28
tmp31 = tmp29 + tmp30
tl.store(out_ptr0 + x0, tmp0, xmask)
tl.store(in_out_ptr0 + (r3 + 16 * x0), tmp3, xmask)
tl.store(out_ptr3 + (r3 + 16 * x0), tmp31, xmask)
tl.store(out_ptr4 + x0, tmp25, xmask)
tl.store(out_ptr1 + x0, tmp13, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7, primals_8, primals_9) = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_3, (4,), (1,))
assert_size_stride(primals_4, (4,), (1,))
assert_size_stride(primals_5, (4,), (1,))
assert_size_stride(primals_6, (4, 4, 3, 3), (36, 9, 3, 1))
assert_size_stride(primals_7, (4,), (1,))
assert_size_stride(primals_8, (4,), (1,))
assert_size_stride(primals_9, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_reflection_pad2d_0[grid(576)](primals_1, buf0, 576,
XBLOCK=128, num_warps=4, num_stages=1)
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1))
buf2 = buf1
del buf1
buf5 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32)
buf6 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
)
buf8 = reinterpret_tensor(buf6, (1, 16, 1, 1), (16, 1, 1, 1), 0)
del buf6
triton_per_fused__native_batch_norm_legit_convolution_1[grid(16)](buf2,
buf8, primals_3, buf5, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((16,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(16)](primals_4, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_4
buf4 = empty_strided_cuda((16,), (1,), torch.float32)
triton_poi_fused_repeat_2[grid(16)](primals_5, buf4, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_5
buf9 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32)
triton_poi_fused_reflection_pad2d_relu_3[grid(576)](buf2, buf5,
buf8, buf3, buf4, buf9, 576, XBLOCK=256, num_warps=4, num_stages=1)
buf10 = extern_kernels.convolution(buf9, primals_6, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf10, (4, 4, 4, 4), (64, 16, 4, 1))
buf12 = empty_strided_cuda((16,), (1,), torch.float32)
buf11 = buf10
del buf10
buf13 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.
float32)
buf17 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
buf16 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.
float32)
triton_per_fused__native_batch_norm_legit_add_convolution_repeat_4[grid
(16)](buf11, primals_8, primals_7, primals_9, primals_1, buf12,
buf13, buf17, buf16, 16, 16, XBLOCK=8, num_warps=2, num_stages=1)
del primals_1
del primals_7
del primals_8
del primals_9
return (buf17, primals_2, primals_6, buf0, buf2, buf3, buf4, buf5, buf8,
buf9, buf11, buf12, reinterpret_tensor(buf16, (16,), (1,), 0),
reinterpret_tensor(buf13, (1, 16, 1, 1), (16, 1, 1, 1), 0))
class ConvLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvLayer, self).__init__()
reflection_padding = 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):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
class ResidualBlockNew(torch.nn.Module):
"""ResidualBlock
introduced in: https://arxiv.org/abs/1512.03385
recommended architecture: http://torch.ch/blog/2016/02/04/resnets.html
"""
def __init__(self, channels):
super(ResidualBlockNew, self).__init__()
self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in1 = torch.nn.InstanceNorm2d(channels, affine=True)
self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in2 = torch.nn.InstanceNorm2d(channels, affine=True)
self.relu = torch.nn.ReLU()
def forward(self, input_0):
primals_2 = self.conv1.conv2d.weight
primals_3 = self.conv1.conv2d.bias
primals_4 = self.in1.weight
primals_5 = self.in1.bias
primals_6 = self.conv2.conv2d.weight
primals_7 = self.conv2.conv2d.bias
primals_8 = self.in2.weight
primals_9 = self.in2.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]
|
EdenBD/MultiModalStory-demo
|
ResidualBlock
| false
| 13,644
|
[
"Apache-2.0"
] | 154
|
5e95e2aca766ca7c850e8db4973b8d51dfdba7f8
|
https://github.com/EdenBD/MultiModalStory-demo/tree/5e95e2aca766ca7c850e8db4973b8d51dfdba7f8
|
FocalFrequencyLoss
|
import torch
import torch.nn as nn
import torch.utils.data
class FocalFrequencyLoss(nn.Module):
"""The torch.nn.Module class that implements focal frequency loss - a
frequency domain loss function for optimizing generative models.
Ref:
Focal Frequency Loss for Image Reconstruction and Synthesis. In ICCV 2021.
<https://arxiv.org/pdf/2012.12821.pdf>
Args:
loss_weight (float): weight for focal frequency loss. Default: 1.0
alpha (float): the scaling factor alpha of the spectrum weight matrix for flexibility. Default: 1.0
patch_factor (int): the factor to crop image patches for patch-based focal frequency loss. Default: 1
ave_spectrum (bool): whether to use minibatch average spectrum. Default: False
log_matrix (bool): whether to adjust the spectrum weight matrix by logarithm. Default: False
batch_matrix (bool): whether to calculate the spectrum weight matrix using batch-based statistics. Default: False
"""
def __init__(self, loss_weight=1.0, alpha=1.0, patch_factor=1,
ave_spectrum=False, log_matrix=False, batch_matrix=False):
super(FocalFrequencyLoss, self).__init__()
self.loss_weight = loss_weight
self.alpha = alpha
self.patch_factor = patch_factor
self.ave_spectrum = ave_spectrum
self.log_matrix = log_matrix
self.batch_matrix = batch_matrix
def tensor2freq(self, x):
patch_factor = self.patch_factor
_, _, h, w = x.shape
assert h % patch_factor == 0 and w % patch_factor == 0, 'Patch factor should be divisible by image height and width'
patch_list = []
patch_h = h // patch_factor
patch_w = w // patch_factor
for i in range(patch_factor):
for j in range(patch_factor):
patch_list.append(x[:, :, i * patch_h:(i + 1) * patch_h, j *
patch_w:(j + 1) * patch_w])
y = torch.stack(patch_list, 1)
if torch.__version__.split('+')[0] > '1.7.1':
freq = torch.fft.fft2(y, norm='ortho')
freq = torch.stack([freq.real, freq.imag], -1)
else:
freq = torch.rfft(y, 2, onesided=False, normalized=True)
return freq
def loss_formulation(self, recon_freq, real_freq, matrix=None):
if matrix is not None:
weight_matrix = matrix.detach()
else:
matrix_tmp = (recon_freq - real_freq) ** 2
matrix_tmp = torch.sqrt(matrix_tmp[..., 0] + matrix_tmp[..., 1]
) ** self.alpha
if self.log_matrix:
matrix_tmp = torch.log(matrix_tmp + 1.0)
if self.batch_matrix:
matrix_tmp = matrix_tmp / matrix_tmp.max()
else:
matrix_tmp = matrix_tmp / matrix_tmp.max(-1).values.max(-1
).values[:, :, :, None, None]
matrix_tmp[torch.isnan(matrix_tmp)] = 0.0
matrix_tmp = torch.clamp(matrix_tmp, min=0.0, max=1.0)
weight_matrix = matrix_tmp.clone().detach()
assert weight_matrix.min().item() >= 0 and weight_matrix.max().item(
) <= 1, 'The values of spectrum weight matrix should be in the range [0, 1], but got Min: %.10f Max: %.10f' % (
weight_matrix.min().item(), weight_matrix.max().item())
tmp = (recon_freq - real_freq) ** 2
freq_distance = tmp[..., 0] + tmp[..., 1]
loss = weight_matrix * freq_distance
return torch.mean(loss)
def forward(self, pred, target, matrix=None, **kwargs):
"""Forward function to calculate focal frequency loss.
Args:
pred (torch.Tensor): of shape (N, C, H, W). Predicted tensor.
target (torch.Tensor): of shape (N, C, H, W). Target tensor.
matrix (torch.Tensor, optional): Element-wise spectrum weight matrix.
Default: None (If set to None: calculated online, dynamic).
"""
pred_freq = self.tensor2freq(pred)
target_freq = self.tensor2freq(target)
if self.ave_spectrum:
pred_freq = torch.mean(pred_freq, 0, keepdim=True)
target_freq = torch.mean(target_freq, 0, keepdim=True)
return self.loss_formulation(pred_freq, target_freq, matrix
) * self.loss_weight
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
import torch.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_stack_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 % 2
x1 = xindex // 2
x2 = xindex
tmp0 = x0
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 1, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + 2 * x1, tmp4 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp6 = tmp0 >= tmp3
tl.full([1], 2, tl.int64)
tmp9 = tl.load(in_ptr1 + (1 + 2 * x1), tmp6 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp10 = tl.where(tmp4, tmp5, tmp9)
tl.store(out_ptr0 + x2, tmp10, xmask)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
buf0 = empty_strided_cuda((4, 1, 4, 4, 4), (64, 64, 16, 4, 1), torch.
complex64)
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0.copy_(reinterpret_tensor(arg0_1, (4, 1, 4, 4, 4), (64, 64, 16,
4, 1), 0))
del arg0_1
buf2 = torch.ops.aten._fft_c2c.default(buf0, [3, 4], 1, True)
del buf0
buf3 = buf2
del buf2
buf4 = torch.ops.aten.view_as_real.default(buf3)
buf5 = buf4
buf6 = torch.ops.aten.view_as_real.default(buf3)
buf7 = buf6
buf8 = empty_strided_cuda((4, 1, 4, 4, 4, 2), (128, 128, 32, 8, 2,
1), torch.float32)
get_raw_stream(0)
triton_poi_fused_stack_0[grid(512)](buf5, buf7, buf8, 512, XBLOCK=
256, num_warps=4, num_stages=1)
del buf4
del buf5
del buf6
del buf7
buf9 = buf3
del buf3
buf9.copy_(reinterpret_tensor(arg1_1, (4, 1, 4, 4, 4), (64, 64, 16,
4, 1), 0))
del arg1_1
buf11 = torch.ops.aten._fft_c2c.default(buf9, [3, 4], 1, True)
del buf9
buf12 = buf11
del buf11
buf13 = torch.ops.aten.view_as_real.default(buf12)
buf14 = buf13
buf15 = torch.ops.aten.view_as_real.default(buf12)
buf16 = buf15
buf17 = empty_strided_cuda((4, 1, 4, 4, 4, 2), (128, 128, 32, 8, 2,
1), torch.float32)
triton_poi_fused_stack_0[grid(512)](buf14, buf16, buf17, 512,
XBLOCK=256, num_warps=4, num_stages=1)
del buf12
del buf13
del buf14
del buf15
del buf16
return buf8, buf17
class FocalFrequencyLossNew(nn.Module):
"""The torch.nn.Module class that implements focal frequency loss - a
frequency domain loss function for optimizing generative models.
Ref:
Focal Frequency Loss for Image Reconstruction and Synthesis. In ICCV 2021.
<https://arxiv.org/pdf/2012.12821.pdf>
Args:
loss_weight (float): weight for focal frequency loss. Default: 1.0
alpha (float): the scaling factor alpha of the spectrum weight matrix for flexibility. Default: 1.0
patch_factor (int): the factor to crop image patches for patch-based focal frequency loss. Default: 1
ave_spectrum (bool): whether to use minibatch average spectrum. Default: False
log_matrix (bool): whether to adjust the spectrum weight matrix by logarithm. Default: False
batch_matrix (bool): whether to calculate the spectrum weight matrix using batch-based statistics. Default: False
"""
def __init__(self, loss_weight=1.0, alpha=1.0, patch_factor=1,
ave_spectrum=False, log_matrix=False, batch_matrix=False):
super(FocalFrequencyLossNew, self).__init__()
self.loss_weight = loss_weight
self.alpha = alpha
self.patch_factor = patch_factor
self.ave_spectrum = ave_spectrum
self.log_matrix = log_matrix
self.batch_matrix = batch_matrix
def tensor2freq(self, x):
patch_factor = self.patch_factor
_, _, h, w = x.shape
assert h % patch_factor == 0 and w % patch_factor == 0, 'Patch factor should be divisible by image height and width'
patch_list = []
patch_h = h // patch_factor
patch_w = w // patch_factor
for i in range(patch_factor):
for j in range(patch_factor):
patch_list.append(x[:, :, i * patch_h:(i + 1) * patch_h, j *
patch_w:(j + 1) * patch_w])
y = torch.stack(patch_list, 1)
if torch.__version__.split('+')[0] > '1.7.1':
freq = torch.fft.fft2(y, norm='ortho')
freq = torch.stack([freq.real, freq.imag], -1)
else:
freq = torch.rfft(y, 2, onesided=False, normalized=True)
return freq
def loss_formulation(self, recon_freq, real_freq, matrix=None):
if matrix is not None:
weight_matrix = matrix.detach()
else:
matrix_tmp = (recon_freq - real_freq) ** 2
matrix_tmp = torch.sqrt(matrix_tmp[..., 0] + matrix_tmp[..., 1]
) ** self.alpha
if self.log_matrix:
matrix_tmp = torch.log(matrix_tmp + 1.0)
if self.batch_matrix:
matrix_tmp = matrix_tmp / matrix_tmp.max()
else:
matrix_tmp = matrix_tmp / matrix_tmp.max(-1).values.max(-1
).values[:, :, :, None, None]
matrix_tmp[torch.isnan(matrix_tmp)] = 0.0
matrix_tmp = torch.clamp(matrix_tmp, min=0.0, max=1.0)
weight_matrix = matrix_tmp.clone().detach()
assert weight_matrix.min().item() >= 0 and weight_matrix.max().item(
) <= 1, 'The values of spectrum weight matrix should be in the range [0, 1], but got Min: %.10f Max: %.10f' % (
weight_matrix.min().item(), weight_matrix.max().item())
tmp = (recon_freq - real_freq) ** 2
freq_distance = tmp[..., 0] + tmp[..., 1]
loss = weight_matrix * freq_distance
return torch.mean(loss)
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EndlessSora/focal-frequency-loss
|
FocalFrequencyLoss
| false
| 13,645
|
[
"MIT"
] | 364
|
dcaa01ecbfbbd9d8f83f7e5993474e1aa087227c
|
https://github.com/EndlessSora/focal-frequency-loss/tree/dcaa01ecbfbbd9d8f83f7e5993474e1aa087227c
|
TAGConv
|
import torch
import torch.nn as nn
class TAGConv(nn.Module):
def __init__(self, in_features, out_features, K=3, bias=True):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.K = K
self.w = nn.Linear(in_features * (self.K + 1), out_features, bias=bias)
def reset_parameters(self):
self.w.reset_parameters()
def forward(self, x, adj):
out = x
xs = [x]
for _ in range(self.K):
out = adj.mm(out)
xs.append(out)
out = self.w(torch.cat(xs, dim=-1))
return out
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 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 + 16 * x1), tmp0, 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 + 16 * x1), tmp0, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 16), (16, 1))
assert_size_stride(primals_4, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, primals_1, out=buf0)
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, buf0, out=buf1)
buf6 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
buf2 = reinterpret_tensor(buf6, (4, 4), (16, 1), 12)
extern_kernels.mm(primals_2, buf1, out=buf2)
del primals_2
buf3 = reinterpret_tensor(buf6, (4, 4), (16, 1), 0)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(16)](primals_1, buf3, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del primals_1
buf4 = reinterpret_tensor(buf6, (4, 4), (16, 1), 4)
triton_poi_fused_cat_1[grid(16)](buf0, buf4, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf0
buf5 = reinterpret_tensor(buf6, (4, 4), (16, 1), 8)
triton_poi_fused_cat_1[grid(16)](buf1, buf5, 16, XBLOCK=16,
num_warps=1, num_stages=1)
buf7 = buf1
del buf1
extern_kernels.addmm(primals_4, buf6, reinterpret_tensor(primals_3,
(16, 4), (1, 16), 0), alpha=1, beta=1, out=buf7)
del primals_3
del primals_4
return buf7, buf6
class TAGConvNew(nn.Module):
def __init__(self, in_features, out_features, K=3, bias=True):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.K = K
self.w = nn.Linear(in_features * (self.K + 1), out_features, bias=bias)
def reset_parameters(self):
self.w.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})'
)
def forward(self, input_0, input_1):
primals_3 = self.w.weight
primals_4 = self.w.bias
primals_1 = input_0
primals_2 = input_1
output = call([primals_1, primals_2, primals_3, primals_4])
return output[0]
|
EdisonLeeeee/GraphGallery
|
TAGConv
| false
| 13,646
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
GlobalAvgPool2d
|
import torch
import torch.nn as nn
class GlobalAvgPool2d(nn.Module):
def __init__(self):
"""Global average pooling over the input's spatial dimensions"""
super(GlobalAvgPool2d, self).__init__()
def forward(self, inputs):
return nn.functional.adaptive_avg_pool2d(inputs, 1).view(inputs.
size(0), -1)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK:
tl.constexpr):
xnumel = 16
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
xmask = xindex < xnumel
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r1 = rindex
x0 = xindex
tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
tmp3 = tl.where(xmask, tmp1, 0)
tmp4 = tl.sum(tmp3, 1)[:, None]
tmp5 = 16.0
tmp6 = tmp4 / tmp5
tl.debug_barrier()
tl.store(in_out_ptr0 + x0, tmp6, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_mean_0[grid(16)](buf1, arg0_1, 16, 16, XBLOCK=1,
num_warps=2, num_stages=1)
del arg0_1
return reinterpret_tensor(buf1, (4, 4), (4, 1), 0),
class GlobalAvgPool2dNew(nn.Module):
def __init__(self):
"""Global average pooling over the input's spatial dimensions"""
super(GlobalAvgPool2dNew, self).__init__()
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Exdenta/torchsat
|
GlobalAvgPool2d
| false
| 13,647
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
DiceBCELoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class DiceBCELoss(nn.Module):
"""
This loss combines Dice loss with the standard binary cross-entropy (BCE) loss that is generally the default for segmentation models.
Combining the two methods allows for some diversity in the loss, while benefitting from the stability of BCE.
"""
def __init__(self, weight=None, size_average=True):
super(DiceBCELoss, self).__init__()
def forward(self, inputs, targets, smooth=1):
inputs = F.sigmoid(inputs)
inputs = inputs.view(-1)
targets = targets.view(-1)
intersection = (inputs * targets).sum()
dice_loss = 1 - (2.0 * intersection + smooth) / (inputs.sum() +
targets.sum() + smooth)
BCE = F.binary_cross_entropy(inputs, targets, reduction='mean')
Dice_BCE = BCE + dice_loss
return Dice_BCE
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_binary_cross_entropy_div_mul_rsub_sum_0(in_out_ptr0,
in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp3 = tl.load(in_ptr1 + r0, None)
tmp1 = 1.0
tmp2 = tmp0 - tmp1
tmp4 = tl.sigmoid(tmp3)
tmp5 = -tmp4
tmp6 = libdevice.log1p(tmp5)
tmp7 = -100.0
tmp8 = triton_helpers.maximum(tmp6, tmp7)
tmp9 = tmp2 * tmp8
tmp10 = tl_math.log(tmp4)
tmp11 = triton_helpers.maximum(tmp10, tmp7)
tmp12 = tmp0 * tmp11
tmp13 = tmp9 - tmp12
tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
tmp17 = tmp4 * tmp0
tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
tmp21 = tl.broadcast_to(tmp4, [RBLOCK])
tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
tmp24 = tl.broadcast_to(tmp0, [RBLOCK])
tmp26 = triton_helpers.promote_to_tensor(tl.sum(tmp24, 0))
tmp27 = 256.0
tmp28 = tmp16 / tmp27
tmp29 = 2.0
tmp30 = tmp20 * tmp29
tmp31 = tmp30 + tmp1
tmp32 = tmp23 + tmp26
tmp33 = tmp32 + tmp1
tmp34 = tmp31 / tmp33
tmp35 = tmp1 - tmp34
tmp36 = tmp28 + tmp35
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp36, 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_binary_cross_entropy_div_mul_rsub_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 DiceBCELossNew(nn.Module):
"""
This loss combines Dice loss with the standard binary cross-entropy (BCE) loss that is generally the default for segmentation models.
Combining the two methods allows for some diversity in the loss, while benefitting from the stability of BCE.
"""
def __init__(self, weight=None, size_average=True):
super(DiceBCELossNew, 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]
|
Exdenta/torchsat
|
DiceBCELoss
| false
| 13,648
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
CharbonnierLoss
|
import torch
import torch.utils.data
import torch.nn as nn
class CharbonnierLoss(nn.Module):
"""Charbonnier Loss (L1)"""
def __init__(self, eps=1e-06):
super(CharbonnierLoss, self).__init__()
self.eps = eps
def forward(self, x, y):
diff = x - y
loss = torch.sum(torch.sqrt(diff * diff + self.eps))
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
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_per_fused_add_mul_sqrt_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 = tmp2 * tmp2
tmp4 = 1e-06
tmp5 = tmp3 + tmp4
tmp6 = libdevice.sqrt(tmp5)
tmp7 = tl.broadcast_to(tmp6, [RBLOCK])
tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp9, 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_mul_sqrt_sub_sum_0[grid(1)](arg0_1, arg1_1,
buf0, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf0,
class CharbonnierLossNew(nn.Module):
"""Charbonnier Loss (L1)"""
def __init__(self, eps=1e-06):
super(CharbonnierLossNew, self).__init__()
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]
|
EvgeneyZ/TMNet
|
CharbonnierLoss
| false
| 13,649
|
[
"Apache-2.0"
] | 90
|
8a42754747c2fa575e9108c13b5018a884f46099
|
https://github.com/EvgeneyZ/TMNet/tree/8a42754747c2fa575e9108c13b5018a884f46099
|
SpectralEigenConv
|
import torch
import torch.nn as nn
class SpectralEigenConv(nn.Module):
def __init__(self, in_features, out_features, bias=False, K=10, alpha=
0.1, **kwargs):
super().__init__()
assert K > 0
self.K = K
self.alpha = alpha
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
def forward(self, x, U, V=None):
"""
x: node attribute matrix
if `V=None`:
U: (N, N) adjacency matrix
else:
U: (N, k) eigenvector matrix
V: (k,) eigenvalue
"""
x = self.w(x)
if V is not None:
V_pow = torch.ones_like(V)
V_out = torch.zeros_like(V)
for _ in range(self.K):
V_pow *= V
V_out += (1 - self.alpha) * V_pow
V_out = V_out / self.K
x_out = U * V_out @ (U.t() @ x) + self.alpha * x
else:
adj = U
x_in = x
x_out = torch.zeros_like(x)
for _ in range(self.K):
x = torch.spmm(adj, x)
x_out += (1 - self.alpha) * x
x_out /= self.K
x_out += self.alpha * x_in
return x_out
def reset_parameters(self):
self.w.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})'
)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_div_mul_0(in_out_ptr0, in_out_ptr1, in_ptr0,
in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8,
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)
tmp3 = tl.load(in_ptr0 + x0, xmask)
tmp6 = tl.load(in_ptr1 + x0, xmask)
tmp9 = tl.load(in_ptr2 + x0, xmask)
tmp12 = tl.load(in_ptr3 + x0, xmask)
tmp15 = tl.load(in_ptr4 + x0, xmask)
tmp18 = tl.load(in_ptr5 + x0, xmask)
tmp21 = tl.load(in_ptr6 + x0, xmask)
tmp24 = tl.load(in_ptr7 + x0, xmask)
tmp27 = tl.load(in_ptr8 + x0, xmask)
tmp32 = tl.load(in_out_ptr1 + x0, xmask)
tmp1 = 0.9
tmp2 = tmp0 * tmp1
tmp4 = tmp3 * tmp1
tmp5 = tmp2 + tmp4
tmp7 = tmp6 * tmp1
tmp8 = tmp5 + tmp7
tmp10 = tmp9 * tmp1
tmp11 = tmp8 + tmp10
tmp13 = tmp12 * tmp1
tmp14 = tmp11 + tmp13
tmp16 = tmp15 * tmp1
tmp17 = tmp14 + tmp16
tmp19 = tmp18 * tmp1
tmp20 = tmp17 + tmp19
tmp22 = tmp21 * tmp1
tmp23 = tmp20 + tmp22
tmp25 = tmp24 * tmp1
tmp26 = tmp23 + tmp25
tmp28 = tmp27 * tmp1
tmp29 = tmp26 + tmp28
tmp30 = 0.1
tmp31 = tmp29 * tmp30
tmp33 = tmp32 * tmp30
tmp34 = tmp31 + tmp33
tl.store(in_out_ptr1 + x0, tmp34, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4), (4, 1))
assert_size_stride(primals_3, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4),
(1, 4), 0), out=buf0)
del primals_1
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf0, out=buf1)
buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf1, out=buf2)
buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf2, out=buf3)
buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf3, out=buf4)
buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf4, out=buf5)
buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf5, out=buf6)
buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf6, out=buf7)
buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf7, out=buf8)
buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf8, out=buf9)
buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(primals_3, buf9, out=buf11)
buf10 = buf1
del buf1
buf12 = buf0
del buf0
get_raw_stream(0)
triton_poi_fused_add_div_mul_0[grid(16)](buf10, buf12, buf2, buf3,
buf4, buf5, buf6, buf7, buf8, buf9, buf11, 16, XBLOCK=16,
num_warps=1, num_stages=1)
del buf10
del buf11
del buf2
del buf3
del buf4
del buf5
del buf6
del buf7
del buf8
del buf9
return buf12, primals_2, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0)
class SpectralEigenConvNew(nn.Module):
def __init__(self, in_features, out_features, bias=False, K=10, alpha=
0.1, **kwargs):
super().__init__()
assert K > 0
self.K = K
self.alpha = alpha
self.in_features = in_features
self.out_features = out_features
self.w = nn.Linear(in_features, out_features, bias=bias)
def reset_parameters(self):
self.w.reset_parameters()
def __repr__(self):
return (
f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})'
)
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]
|
EdisonLeeeee/GraphGallery
|
SpectralEigenConv
| false
| 13,650
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
MaskedMultiTaskCrossEntropy
|
import torch
from torch import nn
class MaskedMultiTaskCrossEntropy(nn.Module):
def forward(self, input, target):
scores = torch.sigmoid(input)
target_active = (target == 1).float()
loss_terms = -(target_active * torch.log(scores) + (1 -
target_active) * torch.log(1 - scores))
missing_values_mask = (target != 0).float()
return (loss_terms * missing_values_mask).sum(
) / missing_values_mask.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
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused__to_copy_add_div_eq_log_mul_ne_neg_rsub_sigmoid_sum_0(
in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp4 = tl.load(in_ptr1 + r0, None)
tmp1 = 1.0
tmp2 = tmp0 == tmp1
tmp3 = tmp2.to(tl.float32)
tmp5 = tl.sigmoid(tmp4)
tmp6 = tl_math.log(tmp5)
tmp7 = tmp3 * tmp6
tmp8 = tmp1 - tmp3
tmp9 = tmp1 - tmp5
tmp10 = tl_math.log(tmp9)
tmp11 = tmp8 * tmp10
tmp12 = tmp7 + tmp11
tmp13 = -tmp12
tmp14 = 0.0
tmp15 = tmp0 != tmp14
tmp16 = tmp15.to(tl.float32)
tmp17 = tmp13 * tmp16
tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
tmp21 = tl.broadcast_to(tmp16, [RBLOCK])
tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
tmp24 = tmp20 / tmp23
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp24, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf2 = buf0
del buf0
get_raw_stream(0)
triton_per_fused__to_copy_add_div_eq_log_mul_ne_neg_rsub_sigmoid_sum_0[
grid(1)](buf2, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf2,
class MaskedMultiTaskCrossEntropyNew(nn.Module):
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EricBoittier/graph-neural-networks-for-drug-discovery
|
MaskedMultiTaskCrossEntropy
| false
| 13,651
|
[
"MIT"
] | 69
|
12fed5c6e7bbd716d9f713d34067ed83dd539b50
|
https://github.com/EricBoittier/graph-neural-networks-for-drug-discovery/tree/12fed5c6e7bbd716d9f713d34067ed83dd539b50
|
IoULoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class IoULoss(nn.Module):
"""
The IoU metric, or Jaccard Index, is similar to the Dice metric and is calculated as the ratio between the overlap
of the positive instances between two sets, and their mutual combined values
"""
def __init__(self, weight=None, size_average=True):
super(IoULoss, self).__init__()
def forward(self, inputs, targets, smooth=1):
inputs = F.sigmoid(inputs)
inputs = inputs.view(-1)
targets = targets.view(-1)
intersection = (inputs * targets).sum()
total = (inputs + targets).sum()
union = total - intersection
IoU = (intersection + smooth) / (union + smooth)
return 1 - IoU
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_div_mul_rsub_sub_sum_0(in_out_ptr0, in_ptr0,
in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp2 = tl.load(in_ptr1 + r0, None)
tmp1 = tl.sigmoid(tmp0)
tmp3 = tmp1 * tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tmp7 = tmp1 + tmp2
tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
tmp11 = 1.0
tmp12 = tmp6 + tmp11
tmp13 = tmp10 - tmp6
tmp14 = tmp13 + tmp11
tmp15 = tmp12 / tmp14
tmp16 = tmp11 - tmp15
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp16, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf2 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_div_mul_rsub_sub_sum_0[grid(1)](buf2, arg0_1,
arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf2,
class IoULossNew(nn.Module):
"""
The IoU metric, or Jaccard Index, is similar to the Dice metric and is calculated as the ratio between the overlap
of the positive instances between two sets, and their mutual combined values
"""
def __init__(self, weight=None, size_average=True):
super(IoULossNew, 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]
|
Exdenta/torchsat
|
IoULoss
| false
| 13,652
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
FocalLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class FocalLoss(nn.Module):
"""
Focal Loss was introduced by Lin et al of Facebook AI Research in 2017 as a means of combatting extremely imbalanced datasets
where positive cases were relatively rare. Their paper "Focal Loss for Dense Object Detection" is retrievable
here: https://arxiv.org/abs/1708.02002. In practice, the researchers used an alpha-modified version of the function
so I have included it in this implementation.
"""
def __init__(self, weight=None, size_average=True):
super(FocalLoss, self).__init__()
def forward(self, inputs, targets, alpha=0.8, gamma=2, smooth=1):
inputs = F.sigmoid(inputs)
inputs = inputs.view(-1)
targets = targets.view(-1)
BCE = F.binary_cross_entropy(inputs, targets, reduction='mean')
BCE_EXP = torch.exp(-BCE)
focal_loss = alpha * (1 - BCE_EXP) ** gamma * BCE
return focal_loss
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_binary_cross_entropy_exp_mul_neg_pow_rsub_0(in_out_ptr0,
in_ptr0, in_ptr1, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp3 = tl.load(in_ptr1 + r0, None)
tmp1 = 1.0
tmp2 = tmp0 - tmp1
tmp4 = tl.sigmoid(tmp3)
tmp5 = -tmp4
tmp6 = libdevice.log1p(tmp5)
tmp7 = -100.0
tmp8 = triton_helpers.maximum(tmp6, tmp7)
tmp9 = tmp2 * tmp8
tmp10 = tl_math.log(tmp4)
tmp11 = triton_helpers.maximum(tmp10, tmp7)
tmp12 = tmp0 * tmp11
tmp13 = tmp9 - tmp12
tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
tmp17 = 256.0
tmp18 = tmp16 / tmp17
tmp19 = -tmp18
tmp20 = tl_math.exp(tmp19)
tmp21 = tmp1 - tmp20
tmp22 = tmp21 * tmp21
tmp23 = 0.8
tmp24 = tmp22 * tmp23
tmp25 = tmp24 * tmp18
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp25, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_binary_cross_entropy_exp_mul_neg_pow_rsub_0[grid(1)](
buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf1,
class FocalLossNew(nn.Module):
"""
Focal Loss was introduced by Lin et al of Facebook AI Research in 2017 as a means of combatting extremely imbalanced datasets
where positive cases were relatively rare. Their paper "Focal Loss for Dense Object Detection" is retrievable
here: https://arxiv.org/abs/1708.02002. In practice, the researchers used an alpha-modified version of the function
so I have included it in this implementation.
"""
def __init__(self, weight=None, size_average=True):
super(FocalLossNew, 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]
|
Exdenta/torchsat
|
FocalLoss
| false
| 13,653
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
rSoftMax
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2)
x = F.softmax(x, dim=1)
x = x.reshape(batch, -1)
else:
x = torch.sigmoid(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'radix': 4, 'cardinality': 4}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 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_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 % 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 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x1 + 64 * x3), xmask,
eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x1 + 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):
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, 4, 16, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused__softmax_0[grid(256)](arg0_1, buf0, 256, XBLOCK=
256, num_warps=4, num_stages=1)
del arg0_1
buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=128,
num_warps=4, num_stages=1)
del buf0
return reinterpret_tensor(buf1, (4, 64), (64, 1), 0),
class rSoftMaxNew(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Exdenta/torchsat
|
rSoftMax
| false
| 13,654
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
DiceLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class DiceLoss(nn.Module):
"""
The Dice coefficient, or Dice-Sørensen coefficient, is a common metric for pixel segmentation
"""
def __init__(self, weight=None, size_average=True):
super(DiceLoss, self).__init__()
def forward(self, inputs, labels, smooth=1):
inputs = F.sigmoid(inputs)
inputs = inputs.view(-1)
labels = labels.view(-1)
intersection = (inputs * labels).sum()
dice = (2.0 * intersection + smooth) / (inputs.sum() + labels.sum() +
smooth)
return 1 - dice
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp2 = tl.load(in_ptr1 + r0, None)
tmp1 = tl.sigmoid(tmp0)
tmp3 = tmp1 * tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tmp7 = tl.broadcast_to(tmp1, [RBLOCK])
tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
tmp10 = tl.broadcast_to(tmp2, [RBLOCK])
tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
tmp13 = 2.0
tmp14 = tmp6 * tmp13
tmp15 = 1.0
tmp16 = tmp14 + tmp15
tmp17 = tmp9 + tmp12
tmp18 = tmp17 + tmp15
tmp19 = tmp16 / tmp18
tmp20 = tmp15 - tmp19
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp20, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf3 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf3, arg0_1,
arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf3,
class DiceLossNew(nn.Module):
"""
The Dice coefficient, or Dice-Sørensen coefficient, is a common metric for pixel segmentation
"""
def __init__(self, weight=None, size_average=True):
super(DiceLossNew, self).__init__()
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
Exdenta/torchsat
|
DiceLoss
| false
| 13,655
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
FocalTverskyLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class FocalTverskyLoss(nn.Module):
"""
A variant on the Tversky loss that also includes the gamma modifier from Focal Loss.
"""
def __init__(self, weight=None, size_average=True):
super(FocalTverskyLoss, self).__init__()
def forward(self, inputs, targets, smooth=1, alpha=0.5, beta=0.5, gamma=1):
inputs = F.sigmoid(inputs)
inputs = inputs.view(-1)
targets = targets.view(-1)
TP = (inputs * targets).sum()
FP = ((1 - targets) * inputs).sum()
FN = (targets * (1 - inputs)).sum()
Tversky = (TP + smooth) / (TP + alpha * FP + beta * FN + smooth)
FocalTversky = (1 - Tversky) ** gamma
return FocalTversky
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_add_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp2 = tl.load(in_ptr1 + r0, None)
tmp1 = tl.sigmoid(tmp0)
tmp3 = tmp1 * tmp2
tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
tmp7 = 1.0
tmp8 = tmp7 - tmp2
tmp9 = tmp8 * tmp1
tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
tmp13 = tmp7 - tmp1
tmp14 = tmp2 * tmp13
tmp15 = tl.broadcast_to(tmp14, [RBLOCK])
tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
tmp18 = tmp6 + tmp7
tmp19 = 0.5
tmp20 = tmp12 * tmp19
tmp21 = tmp6 + tmp20
tmp22 = tmp17 * tmp19
tmp23 = tmp21 + tmp22
tmp24 = tmp23 + tmp7
tmp25 = tmp18 / tmp24
tmp26 = tmp7 - tmp25
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp26, None)
def call(args):
arg0_1, arg1_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf3 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf3, arg0_1,
arg1_1, 1, 256, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
return buf3,
class FocalTverskyLossNew(nn.Module):
"""
A variant on the Tversky loss that also includes the gamma modifier from Focal Loss.
"""
def __init__(self, weight=None, size_average=True):
super(FocalTverskyLossNew, 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]
|
Exdenta/torchsat
|
FocalTverskyLoss
| false
| 13,656
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
ConvInRelu
|
import torch
import numpy as np
import torch.nn as nn
class ConvInRelu(nn.Module):
def __init__(self, channels_in, channels_out, kernel_size, stride=1):
super(ConvInRelu, self).__init__()
self.n_params = 0
self.channels = channels_out
self.reflection_pad = nn.ReflectionPad2d(int(np.floor(kernel_size / 2))
)
self.conv = nn.Conv2d(channels_in, channels_out, kernel_size,
stride, padding=0)
self.instancenorm = nn.InstanceNorm2d(channels_out)
self.relu = nn.ReLU(inplace=False)
def forward(self, x):
x = self.reflection_pad(x)
x = self.conv(x)
x = self.instancenorm(x)
x = self.relu(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'channels_in': 4, 'channels_out': 4, 'kernel_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
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 = 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_per_fused__native_batch_norm_legit_convolution_relu_threshold_backward_1(
in_out_ptr0, in_ptr0, out_ptr0, out_ptr2, out_ptr3, out_ptr4, xnumel,
rnumel, XBLOCK: tl.constexpr):
xnumel = 16
rnumel = 25
RBLOCK: tl.constexpr = 32
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
r2 = rindex
x3 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + (r2 + 25 * x3), rmask & xmask, other=0.0)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
tl.where(rmask & xmask, tmp3, 0)
tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
tmp8 = tl.where(rmask & xmask, tmp6, 0)
tmp9 = tl.sum(tmp8, 1)[:, None]
tmp10 = tl.full([XBLOCK, 1], 25, tl.int32)
tmp11 = tmp10.to(tl.float32)
tmp12 = tmp9 / tmp11
tmp13 = tmp3 - tmp12
tmp14 = tmp13 * tmp13
tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
tmp17 = tl.where(rmask & xmask, tmp15, 0)
tmp18 = tl.sum(tmp17, 1)[:, None]
tmp19 = tmp2 - tmp12
tmp20 = 25.0
tmp21 = tmp18 / tmp20
tmp22 = 1e-05
tmp23 = tmp21 + tmp22
tmp24 = libdevice.rsqrt(tmp23)
tmp25 = tmp19 * tmp24
tmp26 = tl.full([1, 1], 0, tl.int32)
tmp27 = triton_helpers.maximum(tmp26, tmp25)
tmp28 = 0.0
tmp29 = tmp27 <= tmp28
tl.store(in_out_ptr0 + (r2 + 25 * x3), tmp2, rmask & xmask)
tl.store(out_ptr2 + (r2 + 25 * x3), tmp27, rmask & xmask)
tl.store(out_ptr3 + (r2 + 25 * x3), tmp29, rmask & xmask)
tl.store(out_ptr4 + x3, tmp24, xmask)
tl.store(out_ptr0 + x3, tmp12, 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((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
)
buf7 = empty_strided_cuda((4, 4, 5, 5), (100, 25, 5, 1), torch.float32)
buf8 = empty_strided_cuda((4, 4, 5, 5), (100, 25, 5, 1), torch.bool)
buf6 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
)
triton_per_fused__native_batch_norm_legit_convolution_relu_threshold_backward_1[
grid(16)](buf2, primals_3, buf3, buf7, buf8, buf6, 16, 25,
XBLOCK=8, num_warps=2, num_stages=1)
del primals_3
return buf7, primals_2, buf0, buf2, reinterpret_tensor(buf6, (16,), (1,), 0
), buf8, reinterpret_tensor(buf3, (1, 16, 1, 1), (16, 1, 1, 1), 0)
class ConvInReluNew(nn.Module):
def __init__(self, channels_in, channels_out, kernel_size, stride=1):
super(ConvInReluNew, self).__init__()
self.n_params = 0
self.channels = channels_out
self.reflection_pad = nn.ReflectionPad2d(int(np.floor(kernel_size / 2))
)
self.conv = nn.Conv2d(channels_in, channels_out, kernel_size,
stride, padding=0)
self.instancenorm = nn.InstanceNorm2d(channels_out)
self.relu = nn.ReLU(inplace=False)
def forward(self, input_0):
primals_1 = self.conv.weight
primals_3 = self.conv.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
ElistratovSemyon/style-augmentation
|
ConvInRelu
| false
| 13,657
|
[
"MIT"
] | 69
|
ac88dcc92d43615e9a63d90ba58cdd8178c5b02c
|
https://github.com/ElistratovSemyon/style-augmentation/tree/ac88dcc92d43615e9a63d90ba58cdd8178c5b02c
|
LabelPropagation
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class LabelPropagation(nn.Module):
"""label propagation model adapted from https://github.com/CUAI/CorrectAndSmooth
`"Learning from Labeled and
Unlabeled Datawith Label Propagation"
<http://mlg.eng.cam.ac.uk/zoubin/papers/CMU-CALD-02-107.pdf>`_ paper
"""
def __init__(self, num_layers=50, alpha=0.5, residual=True):
super().__init__()
self.num_layers = num_layers
self.alpha = alpha
self.residual = residual
@torch.no_grad()
def forward(self, y, adj, mask=None):
if y.dtype == torch.long:
y = F.one_hot(y.view(-1)).float()
out = y
if mask is not None:
out = torch.zeros_like(y)
out[mask] = y[mask]
if self.residual:
res = (1 - self.alpha) * out
else:
res = out.clone()
for _ in range(self.num_layers):
out = self.alpha * (adj @ out) + res
out = torch.clamp(out, 0, 1)
return out
def get_inputs():
return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_clamp_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_out_ptr0 + x0, xmask)
tmp3 = tl.load(in_ptr0 + x0, xmask)
tmp1 = 0.5
tmp2 = tmp0 * tmp1
tmp4 = tmp3 * tmp1
tmp5 = tmp2 + tmp4
tmp6 = 0.0
tmp7 = triton_helpers.maximum(tmp5, tmp6)
tmp8 = 1.0
tmp9 = triton_helpers.minimum(tmp7, tmp8)
tl.store(in_out_ptr0 + x0, tmp9, 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((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(arg0_1, (16, 4, 4), (16, 4, 1), 0),
out=buf0)
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf0
get_raw_stream(0)
triton_poi_fused_add_clamp_mul_0[grid(256)](buf1, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf2 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0), out
=buf2)
buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf2
triton_poi_fused_add_clamp_mul_0[grid(256)](buf3, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf4 = reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0)
del buf1
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0), out
=buf4)
buf5 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf4
triton_poi_fused_add_clamp_mul_0[grid(256)](buf5, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf6 = reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0)
del buf3
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0), out
=buf6)
buf7 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf6
triton_poi_fused_add_clamp_mul_0[grid(256)](buf7, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf8 = reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0)
del buf5
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), out
=buf8)
buf9 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf8
triton_poi_fused_add_clamp_mul_0[grid(256)](buf9, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf10 = reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0)
del buf7
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out
=buf10)
buf11 = reinterpret_tensor(buf10, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf10
triton_poi_fused_add_clamp_mul_0[grid(256)](buf11, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf12 = reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0)
del buf9
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1), 0),
out=buf12)
buf13 = reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf12
triton_poi_fused_add_clamp_mul_0[grid(256)](buf13, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf14 = reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1), 0)
del buf11
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf13, (16, 4, 4), (16, 4, 1), 0),
out=buf14)
buf15 = reinterpret_tensor(buf14, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf14
triton_poi_fused_add_clamp_mul_0[grid(256)](buf15, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf16 = reinterpret_tensor(buf13, (16, 4, 4), (16, 4, 1), 0)
del buf13
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf15, (16, 4, 4), (16, 4, 1), 0),
out=buf16)
buf17 = reinterpret_tensor(buf16, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf16
triton_poi_fused_add_clamp_mul_0[grid(256)](buf17, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf18 = reinterpret_tensor(buf15, (16, 4, 4), (16, 4, 1), 0)
del buf15
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf17, (16, 4, 4), (16, 4, 1), 0),
out=buf18)
buf19 = reinterpret_tensor(buf18, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf18
triton_poi_fused_add_clamp_mul_0[grid(256)](buf19, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf20 = reinterpret_tensor(buf17, (16, 4, 4), (16, 4, 1), 0)
del buf17
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf19, (16, 4, 4), (16, 4, 1), 0),
out=buf20)
buf21 = reinterpret_tensor(buf20, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf20
triton_poi_fused_add_clamp_mul_0[grid(256)](buf21, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf22 = reinterpret_tensor(buf19, (16, 4, 4), (16, 4, 1), 0)
del buf19
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf21, (16, 4, 4), (16, 4, 1), 0),
out=buf22)
buf23 = reinterpret_tensor(buf22, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf22
triton_poi_fused_add_clamp_mul_0[grid(256)](buf23, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf24 = reinterpret_tensor(buf21, (16, 4, 4), (16, 4, 1), 0)
del buf21
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf23, (16, 4, 4), (16, 4, 1), 0),
out=buf24)
buf25 = reinterpret_tensor(buf24, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf24
triton_poi_fused_add_clamp_mul_0[grid(256)](buf25, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf26 = reinterpret_tensor(buf23, (16, 4, 4), (16, 4, 1), 0)
del buf23
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf25, (16, 4, 4), (16, 4, 1), 0),
out=buf26)
buf27 = reinterpret_tensor(buf26, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf26
triton_poi_fused_add_clamp_mul_0[grid(256)](buf27, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf28 = reinterpret_tensor(buf25, (16, 4, 4), (16, 4, 1), 0)
del buf25
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf27, (16, 4, 4), (16, 4, 1), 0),
out=buf28)
buf29 = reinterpret_tensor(buf28, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf28
triton_poi_fused_add_clamp_mul_0[grid(256)](buf29, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf30 = reinterpret_tensor(buf27, (16, 4, 4), (16, 4, 1), 0)
del buf27
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf29, (16, 4, 4), (16, 4, 1), 0),
out=buf30)
buf31 = reinterpret_tensor(buf30, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf30
triton_poi_fused_add_clamp_mul_0[grid(256)](buf31, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf32 = reinterpret_tensor(buf29, (16, 4, 4), (16, 4, 1), 0)
del buf29
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf31, (16, 4, 4), (16, 4, 1), 0),
out=buf32)
buf33 = reinterpret_tensor(buf32, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf32
triton_poi_fused_add_clamp_mul_0[grid(256)](buf33, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf34 = reinterpret_tensor(buf31, (16, 4, 4), (16, 4, 1), 0)
del buf31
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf33, (16, 4, 4), (16, 4, 1), 0),
out=buf34)
buf35 = reinterpret_tensor(buf34, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf34
triton_poi_fused_add_clamp_mul_0[grid(256)](buf35, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf36 = reinterpret_tensor(buf33, (16, 4, 4), (16, 4, 1), 0)
del buf33
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf35, (16, 4, 4), (16, 4, 1), 0),
out=buf36)
buf37 = reinterpret_tensor(buf36, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf36
triton_poi_fused_add_clamp_mul_0[grid(256)](buf37, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf38 = reinterpret_tensor(buf35, (16, 4, 4), (16, 4, 1), 0)
del buf35
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf37, (16, 4, 4), (16, 4, 1), 0),
out=buf38)
buf39 = reinterpret_tensor(buf38, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf38
triton_poi_fused_add_clamp_mul_0[grid(256)](buf39, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf40 = reinterpret_tensor(buf37, (16, 4, 4), (16, 4, 1), 0)
del buf37
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf39, (16, 4, 4), (16, 4, 1), 0),
out=buf40)
buf41 = reinterpret_tensor(buf40, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf40
triton_poi_fused_add_clamp_mul_0[grid(256)](buf41, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf42 = reinterpret_tensor(buf39, (16, 4, 4), (16, 4, 1), 0)
del buf39
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf41, (16, 4, 4), (16, 4, 1), 0),
out=buf42)
buf43 = reinterpret_tensor(buf42, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf42
triton_poi_fused_add_clamp_mul_0[grid(256)](buf43, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf44 = reinterpret_tensor(buf41, (16, 4, 4), (16, 4, 1), 0)
del buf41
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf43, (16, 4, 4), (16, 4, 1), 0),
out=buf44)
buf45 = reinterpret_tensor(buf44, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf44
triton_poi_fused_add_clamp_mul_0[grid(256)](buf45, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf46 = reinterpret_tensor(buf43, (16, 4, 4), (16, 4, 1), 0)
del buf43
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf45, (16, 4, 4), (16, 4, 1), 0),
out=buf46)
buf47 = reinterpret_tensor(buf46, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf46
triton_poi_fused_add_clamp_mul_0[grid(256)](buf47, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf48 = reinterpret_tensor(buf45, (16, 4, 4), (16, 4, 1), 0)
del buf45
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf47, (16, 4, 4), (16, 4, 1), 0),
out=buf48)
buf49 = reinterpret_tensor(buf48, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf48
triton_poi_fused_add_clamp_mul_0[grid(256)](buf49, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf50 = reinterpret_tensor(buf47, (16, 4, 4), (16, 4, 1), 0)
del buf47
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf49, (16, 4, 4), (16, 4, 1), 0),
out=buf50)
buf51 = reinterpret_tensor(buf50, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf50
triton_poi_fused_add_clamp_mul_0[grid(256)](buf51, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf52 = reinterpret_tensor(buf49, (16, 4, 4), (16, 4, 1), 0)
del buf49
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf51, (16, 4, 4), (16, 4, 1), 0),
out=buf52)
buf53 = reinterpret_tensor(buf52, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf52
triton_poi_fused_add_clamp_mul_0[grid(256)](buf53, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf54 = reinterpret_tensor(buf51, (16, 4, 4), (16, 4, 1), 0)
del buf51
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf53, (16, 4, 4), (16, 4, 1), 0),
out=buf54)
buf55 = reinterpret_tensor(buf54, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf54
triton_poi_fused_add_clamp_mul_0[grid(256)](buf55, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf56 = reinterpret_tensor(buf53, (16, 4, 4), (16, 4, 1), 0)
del buf53
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf55, (16, 4, 4), (16, 4, 1), 0),
out=buf56)
buf57 = reinterpret_tensor(buf56, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf56
triton_poi_fused_add_clamp_mul_0[grid(256)](buf57, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf58 = reinterpret_tensor(buf55, (16, 4, 4), (16, 4, 1), 0)
del buf55
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf57, (16, 4, 4), (16, 4, 1), 0),
out=buf58)
buf59 = reinterpret_tensor(buf58, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf58
triton_poi_fused_add_clamp_mul_0[grid(256)](buf59, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf60 = reinterpret_tensor(buf57, (16, 4, 4), (16, 4, 1), 0)
del buf57
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf59, (16, 4, 4), (16, 4, 1), 0),
out=buf60)
buf61 = reinterpret_tensor(buf60, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf60
triton_poi_fused_add_clamp_mul_0[grid(256)](buf61, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf62 = reinterpret_tensor(buf59, (16, 4, 4), (16, 4, 1), 0)
del buf59
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf61, (16, 4, 4), (16, 4, 1), 0),
out=buf62)
buf63 = reinterpret_tensor(buf62, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf62
triton_poi_fused_add_clamp_mul_0[grid(256)](buf63, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf64 = reinterpret_tensor(buf61, (16, 4, 4), (16, 4, 1), 0)
del buf61
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf63, (16, 4, 4), (16, 4, 1), 0),
out=buf64)
buf65 = reinterpret_tensor(buf64, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf64
triton_poi_fused_add_clamp_mul_0[grid(256)](buf65, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf66 = reinterpret_tensor(buf63, (16, 4, 4), (16, 4, 1), 0)
del buf63
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf65, (16, 4, 4), (16, 4, 1), 0),
out=buf66)
buf67 = reinterpret_tensor(buf66, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf66
triton_poi_fused_add_clamp_mul_0[grid(256)](buf67, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf68 = reinterpret_tensor(buf65, (16, 4, 4), (16, 4, 1), 0)
del buf65
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf67, (16, 4, 4), (16, 4, 1), 0),
out=buf68)
buf69 = reinterpret_tensor(buf68, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf68
triton_poi_fused_add_clamp_mul_0[grid(256)](buf69, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf70 = reinterpret_tensor(buf67, (16, 4, 4), (16, 4, 1), 0)
del buf67
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf69, (16, 4, 4), (16, 4, 1), 0),
out=buf70)
buf71 = reinterpret_tensor(buf70, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf70
triton_poi_fused_add_clamp_mul_0[grid(256)](buf71, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf72 = reinterpret_tensor(buf69, (16, 4, 4), (16, 4, 1), 0)
del buf69
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf71, (16, 4, 4), (16, 4, 1), 0),
out=buf72)
buf73 = reinterpret_tensor(buf72, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf72
triton_poi_fused_add_clamp_mul_0[grid(256)](buf73, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf74 = reinterpret_tensor(buf71, (16, 4, 4), (16, 4, 1), 0)
del buf71
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf73, (16, 4, 4), (16, 4, 1), 0),
out=buf74)
buf75 = reinterpret_tensor(buf74, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf74
triton_poi_fused_add_clamp_mul_0[grid(256)](buf75, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf76 = reinterpret_tensor(buf73, (16, 4, 4), (16, 4, 1), 0)
del buf73
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf75, (16, 4, 4), (16, 4, 1), 0),
out=buf76)
buf77 = reinterpret_tensor(buf76, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf76
triton_poi_fused_add_clamp_mul_0[grid(256)](buf77, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf78 = reinterpret_tensor(buf75, (16, 4, 4), (16, 4, 1), 0)
del buf75
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf77, (16, 4, 4), (16, 4, 1), 0),
out=buf78)
buf79 = reinterpret_tensor(buf78, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf78
triton_poi_fused_add_clamp_mul_0[grid(256)](buf79, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf80 = reinterpret_tensor(buf77, (16, 4, 4), (16, 4, 1), 0)
del buf77
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf79, (16, 4, 4), (16, 4, 1), 0),
out=buf80)
buf81 = reinterpret_tensor(buf80, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf80
triton_poi_fused_add_clamp_mul_0[grid(256)](buf81, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf82 = reinterpret_tensor(buf79, (16, 4, 4), (16, 4, 1), 0)
del buf79
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf81, (16, 4, 4), (16, 4, 1), 0),
out=buf82)
buf83 = reinterpret_tensor(buf82, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf82
triton_poi_fused_add_clamp_mul_0[grid(256)](buf83, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf84 = reinterpret_tensor(buf81, (16, 4, 4), (16, 4, 1), 0)
del buf81
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf83, (16, 4, 4), (16, 4, 1), 0),
out=buf84)
buf85 = reinterpret_tensor(buf84, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf84
triton_poi_fused_add_clamp_mul_0[grid(256)](buf85, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf86 = reinterpret_tensor(buf83, (16, 4, 4), (16, 4, 1), 0)
del buf83
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf85, (16, 4, 4), (16, 4, 1), 0),
out=buf86)
buf87 = reinterpret_tensor(buf86, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf86
triton_poi_fused_add_clamp_mul_0[grid(256)](buf87, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf88 = reinterpret_tensor(buf85, (16, 4, 4), (16, 4, 1), 0)
del buf85
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf87, (16, 4, 4), (16, 4, 1), 0),
out=buf88)
buf89 = reinterpret_tensor(buf88, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf88
triton_poi_fused_add_clamp_mul_0[grid(256)](buf89, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf90 = reinterpret_tensor(buf87, (16, 4, 4), (16, 4, 1), 0)
del buf87
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf89, (16, 4, 4), (16, 4, 1), 0),
out=buf90)
buf91 = reinterpret_tensor(buf90, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf90
triton_poi_fused_add_clamp_mul_0[grid(256)](buf91, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf92 = reinterpret_tensor(buf89, (16, 4, 4), (16, 4, 1), 0)
del buf89
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf91, (16, 4, 4), (16, 4, 1), 0),
out=buf92)
buf93 = reinterpret_tensor(buf92, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf92
triton_poi_fused_add_clamp_mul_0[grid(256)](buf93, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf94 = reinterpret_tensor(buf91, (16, 4, 4), (16, 4, 1), 0)
del buf91
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf93, (16, 4, 4), (16, 4, 1), 0),
out=buf94)
buf95 = reinterpret_tensor(buf94, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf94
triton_poi_fused_add_clamp_mul_0[grid(256)](buf95, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf96 = reinterpret_tensor(buf93, (16, 4, 4), (16, 4, 1), 0)
del buf93
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf95, (16, 4, 4), (16, 4, 1), 0),
out=buf96)
buf97 = reinterpret_tensor(buf96, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf96
triton_poi_fused_add_clamp_mul_0[grid(256)](buf97, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
buf98 = reinterpret_tensor(buf95, (16, 4, 4), (16, 4, 1), 0)
del buf95
extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1
), 0), reinterpret_tensor(buf97, (16, 4, 4), (16, 4, 1), 0),
out=buf98)
del arg1_1
del buf97
buf99 = reinterpret_tensor(buf98, (4, 4, 4, 4), (64, 16, 4, 1), 0)
del buf98
triton_poi_fused_add_clamp_mul_0[grid(256)](buf99, arg0_1, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf99,
class LabelPropagationNew(nn.Module):
"""label propagation model adapted from https://github.com/CUAI/CorrectAndSmooth
`"Learning from Labeled and
Unlabeled Datawith Label Propagation"
<http://mlg.eng.cam.ac.uk/zoubin/papers/CMU-CALD-02-107.pdf>`_ paper
"""
def __init__(self, num_layers=50, alpha=0.5, residual=True):
super().__init__()
self.num_layers = num_layers
self.alpha = alpha
self.residual = residual
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
EdisonLeeeee/GraphGallery
|
LabelPropagation
| false
| 13,658
|
[
"MIT"
] | 300
|
4eec9c5136bda14809bd22584b26cc346cdb633b
|
https://github.com/EdisonLeeeee/GraphGallery/tree/4eec9c5136bda14809bd22584b26cc346cdb633b
|
MaxPool2dDynamicSamePadding
|
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class MaxPool2dDynamicSamePadding(nn.MaxPool2d):
"""2D MaxPooling like TensorFlow's 'SAME' mode, with a dynamic image size.
The padding is operated in forward function by calculating dynamically.
"""
def __init__(self, kernel_size, stride, padding=0, dilation=1,
return_indices=False, ceil_mode=False):
super().__init__(kernel_size, stride, padding, dilation,
return_indices, ceil_mode)
self.stride = [self.stride] * 2 if isinstance(self.stride, int
) else self.stride
self.kernel_size = [self.kernel_size] * 2 if isinstance(self.
kernel_size, int) else self.kernel_size
self.dilation = [self.dilation] * 2 if isinstance(self.dilation, int
) else self.dilation
def forward(self, x):
ih, iw = x.size()[-2:]
kh, kw = self.kernel_size
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] + (kh - 1) * self.dilation[0] +
1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] + (kw - 1) * self.dilation[1] +
1 - iw, 0)
if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h -
pad_h // 2])
return F.max_pool2d(x, self.kernel_size, self.stride, self.padding,
self.dilation, self.ceil_mode, self.return_indices)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'kernel_size': 4, 'stride': 1}]
|
import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
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_max_pool2d_with_indices_0(in_ptr0, out_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 4 % 4
x0 = xindex % 4
x4 = xindex
tmp0 = -1 + x1
tmp1 = tl.full([1], 0, tl.int64)
tmp2 = tmp0 >= tmp1
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = -1 + x0
tmp6 = tmp5 >= tmp1
tmp7 = tmp5 < tmp3
tmp8 = tmp2 & tmp4
tmp9 = tmp8 & tmp6
tmp10 = tmp9 & tmp7
tmp11 = tl.load(in_ptr0 + (-5 + x4), tmp10 & xmask, other=0.0)
tmp12 = x0
tmp13 = tmp12 >= tmp1
tmp14 = tmp12 < tmp3
tmp15 = tmp8 & tmp13
tmp16 = tmp15 & tmp14
tmp17 = tl.load(in_ptr0 + (-4 + x4), tmp16 & xmask, other=0.0)
tmp18 = triton_helpers.maximum(tmp17, tmp11)
tmp19 = 1 + x0
tmp20 = tmp19 >= tmp1
tmp21 = tmp19 < tmp3
tmp22 = tmp8 & tmp20
tmp23 = tmp22 & tmp21
tmp24 = tl.load(in_ptr0 + (-3 + x4), tmp23 & xmask, other=0.0)
tmp25 = triton_helpers.maximum(tmp24, tmp18)
tmp26 = 2 + x0
tmp27 = tmp26 >= tmp1
tmp28 = tmp26 < tmp3
tmp29 = tmp8 & tmp27
tmp30 = tmp29 & tmp28
tmp31 = tl.load(in_ptr0 + (-2 + x4), tmp30 & xmask, other=0.0)
tmp32 = triton_helpers.maximum(tmp31, tmp25)
tmp33 = x1
tmp34 = tmp33 >= tmp1
tmp35 = tmp33 < tmp3
tmp36 = tmp34 & tmp35
tmp37 = tmp36 & tmp6
tmp38 = tmp37 & tmp7
tmp39 = tl.load(in_ptr0 + (-1 + x4), tmp38 & xmask, other=0.0)
tmp40 = triton_helpers.maximum(tmp39, tmp32)
tmp41 = tmp36 & tmp13
tmp42 = tmp41 & tmp14
tmp43 = tl.load(in_ptr0 + x4, tmp42 & xmask, other=0.0)
tmp44 = triton_helpers.maximum(tmp43, tmp40)
tmp45 = tmp36 & tmp20
tmp46 = tmp45 & tmp21
tmp47 = tl.load(in_ptr0 + (1 + x4), tmp46 & xmask, other=0.0)
tmp48 = triton_helpers.maximum(tmp47, tmp44)
tmp49 = tmp36 & tmp27
tmp50 = tmp49 & tmp28
tmp51 = tl.load(in_ptr0 + (2 + x4), tmp50 & xmask, other=0.0)
tmp52 = triton_helpers.maximum(tmp51, tmp48)
tmp53 = 1 + x1
tmp54 = tmp53 >= tmp1
tmp55 = tmp53 < tmp3
tmp56 = tmp54 & tmp55
tmp57 = tmp56 & tmp6
tmp58 = tmp57 & tmp7
tmp59 = tl.load(in_ptr0 + (3 + x4), tmp58 & xmask, other=0.0)
tmp60 = triton_helpers.maximum(tmp59, tmp52)
tmp61 = tmp56 & tmp13
tmp62 = tmp61 & tmp14
tmp63 = tl.load(in_ptr0 + (4 + x4), tmp62 & xmask, other=0.0)
tmp64 = triton_helpers.maximum(tmp63, tmp60)
tmp65 = tmp56 & tmp20
tmp66 = tmp65 & tmp21
tmp67 = tl.load(in_ptr0 + (5 + x4), tmp66 & xmask, other=0.0)
tmp68 = triton_helpers.maximum(tmp67, tmp64)
tmp69 = tmp56 & tmp27
tmp70 = tmp69 & tmp28
tmp71 = tl.load(in_ptr0 + (6 + x4), tmp70 & xmask, other=0.0)
tmp72 = triton_helpers.maximum(tmp71, tmp68)
tmp73 = 2 + x1
tmp74 = tmp73 >= tmp1
tmp75 = tmp73 < tmp3
tmp76 = tmp74 & tmp75
tmp77 = tmp76 & tmp6
tmp78 = tmp77 & tmp7
tmp79 = tl.load(in_ptr0 + (7 + x4), tmp78 & xmask, other=0.0)
tmp80 = triton_helpers.maximum(tmp79, tmp72)
tmp81 = tmp76 & tmp13
tmp82 = tmp81 & tmp14
tmp83 = tl.load(in_ptr0 + (8 + x4), tmp82 & xmask, other=0.0)
tmp84 = triton_helpers.maximum(tmp83, tmp80)
tmp85 = tmp76 & tmp20
tmp86 = tmp85 & tmp21
tmp87 = tl.load(in_ptr0 + (9 + x4), tmp86 & xmask, other=0.0)
tmp88 = triton_helpers.maximum(tmp87, tmp84)
tmp89 = tmp76 & tmp27
tmp90 = tmp89 & tmp28
tmp91 = tl.load(in_ptr0 + (10 + x4), tmp90 & xmask, other=0.0)
tmp92 = triton_helpers.maximum(tmp91, tmp88)
tl.store(out_ptr0 + x4, tmp92, xmask)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_max_pool2d_with_indices_0[grid(256)](arg0_1, buf0,
256, XBLOCK=256, num_warps=4, num_stages=1)
del arg0_1
return buf0,
class MaxPool2dDynamicSamePaddingNew(nn.MaxPool2d):
"""2D MaxPooling like TensorFlow's 'SAME' mode, with a dynamic image size.
The padding is operated in forward function by calculating dynamically.
"""
def __init__(self, kernel_size, stride, padding=0, dilation=1,
return_indices=False, ceil_mode=False):
super().__init__(kernel_size, stride, padding, dilation,
return_indices, ceil_mode)
self.stride = [self.stride] * 2 if isinstance(self.stride, int
) else self.stride
self.kernel_size = [self.kernel_size] * 2 if isinstance(self.
kernel_size, int) else self.kernel_size
self.dilation = [self.dilation] * 2 if isinstance(self.dilation, int
) else self.dilation
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Exdenta/torchsat
|
MaxPool2dDynamicSamePadding
| false
| 13,659
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
Conv2dDynamicSamePadding
|
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class Conv2dDynamicSamePadding(nn.Conv2d):
"""2D Convolutions like TensorFlow, for a dynamic image size.
The padding is operated in forward function by calculating dynamically.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0,
dilation, groups, bias)
self.stride = self.stride if len(self.stride) == 2 else [self.stride[0]
] * 2
def forward(self, x):
ih, iw = x.size()[-2:]
kh, kw = self.weight.size()[-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
pad_h = max((oh - 1) * self.stride[0] + (kh - 1) * self.dilation[0] +
1 - ih, 0)
pad_w = max((ow - 1) * self.stride[1] + (kw - 1) * self.dilation[1] +
1 - iw, 0)
if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h -
pad_h // 2])
return F.conv2d(x, self.weight, self.bias, self.stride, self.
padding, self.dilation, self.groups)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
xnumel = 784
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x1 = xindex // 7 % 7
x0 = xindex % 7
x2 = xindex // 49
x4 = xindex
tmp0 = -1 + x1
tmp1 = tl.full([1], 0, tl.int64)
tmp2 = tmp0 >= tmp1
tmp3 = tl.full([1], 4, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = -1 + x0
tmp6 = tmp5 >= tmp1
tmp7 = tmp5 < tmp3
tmp8 = tmp2 & tmp4
tmp9 = tmp8 & tmp6
tmp10 = tmp9 & tmp7
tmp11 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp10 & xmask,
other=0.0)
tl.store(out_ptr0 + x4, tmp11, xmask)
@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 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, 7, 7), (196, 49, 7, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_constant_pad_nd_0[grid(784)](primals_1, buf0, 784,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(256)](buf2, primals_3, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_3
return buf2, primals_2, buf0
class Conv2dDynamicSamePaddingNew(nn.Conv2d):
"""2D Convolutions like TensorFlow, for a dynamic image size.
The padding is operated in forward function by calculating dynamically.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0,
dilation, groups, bias)
self.stride = self.stride if len(self.stride) == 2 else [self.stride[0]
] * 2
def forward(self, input_0):
primals_1 = self.weight
primals_3 = self.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
Exdenta/torchsat
|
Conv2dDynamicSamePadding
| false
| 13,660
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
outconv
|
import torch
import torch.nn as nn
class outconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(outconv, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 1, padding_mode='reflect')
def forward(self, x):
x = self.conv(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_ch': 4, 'out_ch': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
tl.constexpr):
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
x3 = xindex
tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + x0) + -4 * tl_math
.abs(-3 + x1) + 16 * x2), xmask)
tl.store(out_ptr0 + x3, tmp0, xmask)
@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x1 = xindex // 16 % 4
tmp0 = tl.load(in_out_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x3, tmp2, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 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))
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_reflection_pad2d_0[grid(256)](primals_3, buf0, 256,
XBLOCK=256, num_warps=4, num_stages=1)
del primals_3
buf1 = extern_kernels.convolution(buf0, primals_1, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_1[grid(256)](buf2, primals_2, 256,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
return buf2, primals_1, buf0
class outconvNew(nn.Module):
def __init__(self, in_ch, out_ch):
super(outconvNew, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 1, padding_mode='reflect')
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]
|
ExplorativeEngineering/LFMNet2
|
outconv
| false
| 13,661
|
[
"Apache-2.0"
] | 46
|
3f190be0f047b9e05c69b0a11f99218fd4fc510c
|
https://github.com/ExplorativeEngineering/LFMNet2/tree/3f190be0f047b9e05c69b0a11f99218fd4fc510c
|
SplAtConv2d
|
from torch.nn import Module
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Conv2d
from torch.nn import ReLU
from torch.nn.modules.utils import _pair
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2)
x = F.softmax(x, dim=1)
x = x.reshape(batch, -1)
else:
x = torch.sigmoid(x)
return x
class SplAtConv2d(Module):
"""Split-Attention Conv2d
"""
def __init__(self, in_channels, channels, kernel_size, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), groups=1, bias=True, radix=2,
reduction_factor=4, rectify=False, rectify_avg=False, norm_layer=
None, dropblock_prob=0.0, **kwargs):
super(SplAtConv2d, self).__init__()
padding = _pair(padding)
self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
self.rectify_avg = rectify_avg
inter_channels = max(in_channels * radix // reduction_factor, 32)
self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels * radix, kernel_size,
stride, padding, dilation, groups=groups * radix, bias=bias,
average_mode=rectify_avg, **kwargs)
else:
self.conv = Conv2d(in_channels, channels * radix, kernel_size,
stride, padding, dilation, groups=groups * radix, bias=bias,
**kwargs)
self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels * radix)
self.relu = ReLU(inplace=True)
self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
if self.use_bn:
self.bn1 = norm_layer(inter_channels)
self.fc2 = Conv2d(inter_channels, channels * radix, 1, groups=self.
cardinality)
if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
self.rsoftmax = rSoftMax(radix, groups)
def forward(self, x):
x = self.conv(x)
if self.use_bn:
x = self.bn0(x)
if self.dropblock_prob > 0.0:
x = self.dropblock(x)
x = self.relu(x)
batch, rchannel = x.shape[:2]
if self.radix > 1:
if torch.__version__ < '1.5':
splited = torch.split(x, int(rchannel // self.radix), dim=1)
else:
splited = torch.split(x, rchannel // self.radix, dim=1)
gap = sum(splited)
else:
gap = x
gap = F.adaptive_avg_pool2d(gap, 1)
gap = self.fc1(gap)
if self.use_bn:
gap = self.bn1(gap)
gap = self.relu(gap)
atten = self.fc2(gap)
atten = self.rsoftmax(atten).view(batch, -1, 1, 1)
if self.radix > 1:
if torch.__version__ < '1.5':
attens = torch.split(atten, int(rchannel // self.radix), dim=1)
else:
attens = torch.split(atten, rchannel // self.radix, dim=1)
out = sum([(att * split) for att, split in zip(attens, splited)])
else:
out = atten * x
return out.contiguous()
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_channels': 4, 'channels': 4, 'kernel_size': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch.nn import Module
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Conv2d
from torch.nn import ReLU
from torch.nn.modules.utils import _pair
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_convolution_relu_threshold_backward_0(in_out_ptr0,
in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 32
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 8
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, xmask)
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_add_mean_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask)
tmp3 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
tmp1 = 0.0
tmp2 = tmp0 + tmp1
tmp4 = tmp2 + tmp3
tmp5 = 1.0
tmp6 = tmp4 / tmp5
tl.store(out_ptr0 + x2, tmp6, xmask)
@triton.jit
def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel,
XBLOCK: tl.constexpr):
xnumel = 128
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 32
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tl.store(in_out_ptr0 + x2, tmp4, xmask)
@triton.jit
def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
.constexpr):
xnumel = 32
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 8
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tl.store(in_out_ptr0 + x2, tmp2, xmask)
@triton.jit
def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
):
xnumel = 32
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 4
x2 = xindex // 8
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 8 * x2), xmask, eviction_policy='evict_last'
)
tmp2 = tl.load(in_ptr0 + (4 + x0 + 8 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = triton_helpers.maximum(tmp1, tmp2)
tmp4 = tmp0 - tmp3
tmp5 = tl_math.exp(tmp4)
tmp6 = tmp1 - tmp3
tmp7 = tl_math.exp(tmp6)
tmp8 = tmp2 - tmp3
tmp9 = tl_math.exp(tmp8)
tmp10 = tmp7 + tmp9
tmp11 = tmp5 / tmp10
tl.store(out_ptr0 + x3, tmp11, xmask)
@triton.jit
def triton_poi_fused_add_mul_5(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 % 4
x1 = xindex // 4
x2 = xindex
tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask)
tmp1 = tl.load(in_ptr1 + (x0 + 8 * x1), xmask)
tmp5 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
tmp6 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask)
tmp2 = tmp0 * tmp1
tmp3 = 0.0
tmp4 = tmp2 + tmp3
tmp7 = tmp5 * tmp6
tmp8 = tmp4 + tmp7
tl.store(out_ptr0 + x2, tmp8, xmask)
def call(args):
(primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
primals_7) = args
args.clear()
assert_size_stride(primals_1, (8, 2, 4, 4), (32, 16, 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, (32, 4, 1, 1), (4, 1, 1, 1))
assert_size_stride(primals_5, (32,), (1,))
assert_size_stride(primals_6, (8, 32, 1, 1), (32, 1, 1, 1))
assert_size_stride(primals_7, (8,), (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=2, bias=None)
assert_size_stride(buf0, (4, 8, 1, 1), (8, 1, 1, 1))
buf1 = reinterpret_tensor(buf0, (4, 8, 1, 1), (8, 1, 32, 32), 0)
del buf0
buf9 = empty_strided_cuda((4, 8, 1, 1), (8, 1, 1, 1), torch.bool)
get_raw_stream(0)
triton_poi_fused_convolution_relu_threshold_backward_0[grid(32)](buf1,
primals_2, buf9, 32, XBLOCK=32, num_warps=1, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
triton_poi_fused_add_mean_1[grid(16)](buf1, buf2, 16, XBLOCK=16,
num_warps=1, num_stages=1)
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, 32, 1, 1), (32, 1, 1, 1))
buf4 = buf3
del buf3
triton_poi_fused_convolution_relu_2[grid(128)](buf4, primals_5, 128,
XBLOCK=128, num_warps=4, num_stages=1)
del primals_5
buf5 = extern_kernels.convolution(buf4, primals_6, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), transposed=False,
output_padding=(0, 0), groups=1, bias=None)
assert_size_stride(buf5, (4, 8, 1, 1), (8, 1, 1, 1))
buf6 = buf5
del buf5
triton_poi_fused_convolution_3[grid(32)](buf6, primals_7, 32,
XBLOCK=32, num_warps=1, num_stages=1)
del primals_7
buf7 = empty_strided_cuda((4, 2, 1, 4), (8, 4, 4, 1), torch.float32)
triton_poi_fused__softmax_4[grid(32)](buf6, buf7, 32, XBLOCK=32,
num_warps=1, num_stages=1)
buf8 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
triton_poi_fused_add_mul_5[grid(16)](buf7, buf1, buf8, 16, XBLOCK=
16, num_warps=1, num_stages=1)
return (buf8, primals_1, primals_3, primals_4, primals_6,
reinterpret_tensor(buf1, (4, 4, 1, 1), (8, 1, 1, 1), 0),
reinterpret_tensor(buf1, (4, 4, 1, 1), (8, 1, 1, 1), 4), buf2, buf4,
buf6, reinterpret_tensor(buf7, (4, 4, 1, 1), (8, 1, 1, 1), 0),
reinterpret_tensor(buf7, (4, 4, 1, 1), (8, 1, 1, 1), 4), buf9)
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2)
x = F.softmax(x, dim=1)
x = x.reshape(batch, -1)
else:
x = torch.sigmoid(x)
return x
class SplAtConv2dNew(Module):
"""Split-Attention Conv2d
"""
def __init__(self, in_channels, channels, kernel_size, stride=(1, 1),
padding=(0, 0), dilation=(1, 1), groups=1, bias=True, radix=2,
reduction_factor=4, rectify=False, rectify_avg=False, norm_layer=
None, dropblock_prob=0.0, **kwargs):
super(SplAtConv2dNew, self).__init__()
padding = _pair(padding)
self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
self.rectify_avg = rectify_avg
inter_channels = max(in_channels * radix // reduction_factor, 32)
self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels * radix, kernel_size,
stride, padding, dilation, groups=groups * radix, bias=bias,
average_mode=rectify_avg, **kwargs)
else:
self.conv = Conv2d(in_channels, channels * radix, kernel_size,
stride, padding, dilation, groups=groups * radix, bias=bias,
**kwargs)
self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels * radix)
self.relu = ReLU(inplace=True)
self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
if self.use_bn:
self.bn1 = norm_layer(inter_channels)
self.fc2 = Conv2d(inter_channels, channels * radix, 1, groups=self.
cardinality)
if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
self.rsoftmax = rSoftMax(radix, groups)
def forward(self, input_0):
primals_1 = self.conv.weight
primals_2 = self.conv.bias
primals_4 = self.fc1.weight
primals_5 = self.fc1.bias
primals_6 = self.fc2.weight
primals_7 = self.fc2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4,
primals_5, primals_6, primals_7])
return output[0]
|
Exdenta/torchsat
|
SplAtConv2d
| false
| 13,662
|
[
"MIT"
] | 316
|
70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
https://github.com/Exdenta/torchsat/tree/70ea3db758757104fb3ba618ddf7997f0f3a75b4
|
VitMlpHead
|
import torch
def get_args():
parser = argparse.ArgumentParser()
group = parser.add_argument_group(title='input data')
group.add_argument('--input', type=str, required=True, help=
'Path to input JSON')
group.add_argument('--json-keys', nargs='+', default=['text'], help=
'space separate listed of keys to extract from json')
group.add_argument('--split-sentences', action='store_true', help=
'Split documents into sentences.')
group.add_argument('--keep-newlines', action='store_true', help=
'Keep newlines between sentences when splitting.')
group = parser.add_argument_group(title='tokenizer')
group.add_argument('--tokenizer-type', type=str, required=True, choices
=['BertWordPieceLowerCase', 'BertWordPieceCase', 'GPT2BPETokenizer'
], help='What type of tokenizer to use.')
group.add_argument('--vocab-file', type=str, default=None, help=
'Path to the vocab file')
group.add_argument('--merge-file', type=str, default=None, help=
'Path to the BPE merge file (if necessary).')
group.add_argument('--append-eod', action='store_true', help=
'Append an <eod> token to the end of a document.')
group = parser.add_argument_group(title='output data')
group.add_argument('--output-prefix', type=str, required=True, help=
'Path to binary output file without suffix')
group.add_argument('--dataset-impl', type=str, default='mmap', choices=
['lazy', 'cached', 'mmap'])
group = parser.add_argument_group(title='runtime')
group.add_argument('--workers', type=int, default=1, help=
'Number of worker processes to launch')
group.add_argument('--log-interval', type=int, default=100, help=
'Interval between progress updates')
args = parser.parse_args()
args.keep_empty = False
if args.tokenizer_type.lower().startswith('bert'):
if not args.split_sentences:
None
args.rank = 0
args.make_vocab_size_divisible_by = 128
args.tensor_model_parallel_size = 1
args.vocab_extra_ids = 0
return args
def init_method_normal(sigma):
"""Init method based on N(0, sigma)."""
def init_(tensor):
return torch.nn.init.normal_(tensor, mean=0.0, std=sigma)
return init_
class MegatronModule(torch.nn.Module):
"""Megatron specific extensions of torch Module with support
for pipelining."""
def __init__(self, share_word_embeddings=True):
super(MegatronModule, self).__init__()
self.share_word_embeddings = share_word_embeddings
def state_dict_for_save_checkpoint(self, destination=None, prefix='',
keep_vars=False):
"""Use this function to override the state dict for
saving checkpoints."""
return self.state_dict(destination, prefix, keep_vars)
def word_embeddings_weight(self):
if mpu.is_pipeline_first_stage(ignore_virtual=True):
return self.language_model.embedding.word_embeddings.weight
if mpu.is_pipeline_last_stage(ignore_virtual=True):
if not self.share_word_embeddings:
raise Exception(
'word_embeddings_weight() called for last stage, but share_word_embeddings is false'
)
return self.word_embeddings.weight
raise Exception(
'word_embeddings_weight() should be called for first and last stage only'
)
def initialize_word_embeddings(self, init_method_normal):
args = get_args()
if not self.share_word_embeddings:
raise Exception(
'initialize_word_embeddings() was called but share_word_embeddings is false'
)
if args.pipeline_model_parallel_size == 1:
return
if mpu.is_pipeline_last_stage():
assert not mpu.is_pipeline_first_stage()
self._word_embeddings_for_head_key = 'word_embeddings_for_head'
self.word_embeddings = mpu.VocabParallelEmbedding(args.
padded_vocab_size, args.hidden_size, init_method=
init_method_normal(args.init_method_std))
self.word_embeddings.weight.data.fill_(0)
self.word_embeddings.weight.shared = True
if torch.distributed.is_initialized():
if mpu.is_pipeline_first_stage() or mpu.is_pipeline_last_stage():
torch.distributed.all_reduce(self.word_embeddings_weight().
data, group=mpu.get_embedding_group())
else:
None
class VitMlpHead(MegatronModule):
"""Pooler layer.
Pool hidden states of a specific token (for example start of the
sequence) and add a linear transformation followed by a tanh.
Arguments:
hidden_size: hidden size
init_method: weight initialization method for the linear layer.
bias is set to zero.
"""
def __init__(self, hidden_size, num_classes):
super(VitMlpHead, self).__init__()
self.dense_in = torch.nn.Linear(hidden_size, hidden_size)
self.dense_out = torch.nn.Linear(hidden_size, num_classes)
torch.nn.init.constant_(self.dense_out.bias, -10)
def forward(self, hidden_states, sequence_index=0):
x = hidden_states[:, sequence_index, :]
x = self.dense_in(x)
x = torch.tanh(x)
x = self.dense_out(x)
return x
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'hidden_size': 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
from torch._inductor.runtime.triton_helpers import libdevice
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_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_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 64
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x0 = xindex % 4
tmp0 = tl.load(in_out_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = libdevice.tanh(tmp2)
tl.store(in_out_ptr0 + x2, tmp3, xmask)
def call(args):
primals_1, primals_2, primals_3, primals_4, primals_5 = args
args.clear()
assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_2, (4, 4), (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,))
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
triton_poi_fused_add_tanh_1[grid(64)](buf2, primals_3, 64, XBLOCK=
64, num_warps=1, num_stages=1)
del primals_3
buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (16, 4), (
4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0),
alpha=1, beta=1, out=buf3)
del primals_5
return reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0
), reinterpret_tensor(buf0, (16, 4), (4, 1), 0), buf2, primals_4
def get_args():
parser = argparse.ArgumentParser()
group = parser.add_argument_group(title='input data')
group.add_argument('--input', type=str, required=True, help=
'Path to input JSON')
group.add_argument('--json-keys', nargs='+', default=['text'], help=
'space separate listed of keys to extract from json')
group.add_argument('--split-sentences', action='store_true', help=
'Split documents into sentences.')
group.add_argument('--keep-newlines', action='store_true', help=
'Keep newlines between sentences when splitting.')
group = parser.add_argument_group(title='tokenizer')
group.add_argument('--tokenizer-type', type=str, required=True, choices
=['BertWordPieceLowerCase', 'BertWordPieceCase', 'GPT2BPETokenizer'
], help='What type of tokenizer to use.')
group.add_argument('--vocab-file', type=str, default=None, help=
'Path to the vocab file')
group.add_argument('--merge-file', type=str, default=None, help=
'Path to the BPE merge file (if necessary).')
group.add_argument('--append-eod', action='store_true', help=
'Append an <eod> token to the end of a document.')
group = parser.add_argument_group(title='output data')
group.add_argument('--output-prefix', type=str, required=True, help=
'Path to binary output file without suffix')
group.add_argument('--dataset-impl', type=str, default='mmap', choices=
['lazy', 'cached', 'mmap'])
group = parser.add_argument_group(title='runtime')
group.add_argument('--workers', type=int, default=1, help=
'Number of worker processes to launch')
group.add_argument('--log-interval', type=int, default=100, help=
'Interval between progress updates')
args = parser.parse_args()
args.keep_empty = False
if args.tokenizer_type.lower().startswith('bert'):
if not args.split_sentences:
None
args.rank = 0
args.make_vocab_size_divisible_by = 128
args.tensor_model_parallel_size = 1
args.vocab_extra_ids = 0
return args
def init_method_normal(sigma):
"""Init method based on N(0, sigma)."""
def init_(tensor):
return torch.nn.init.normal_(tensor, mean=0.0, std=sigma)
return init_
class MegatronModule(torch.nn.Module):
"""Megatron specific extensions of torch Module with support
for pipelining."""
def __init__(self, share_word_embeddings=True):
super(MegatronModule, self).__init__()
self.share_word_embeddings = share_word_embeddings
def state_dict_for_save_checkpoint(self, destination=None, prefix='',
keep_vars=False):
"""Use this function to override the state dict for
saving checkpoints."""
return self.state_dict(destination, prefix, keep_vars)
def word_embeddings_weight(self):
if mpu.is_pipeline_first_stage(ignore_virtual=True):
return self.language_model.embedding.word_embeddings.weight
if mpu.is_pipeline_last_stage(ignore_virtual=True):
if not self.share_word_embeddings:
raise Exception(
'word_embeddings_weight() called for last stage, but share_word_embeddings is false'
)
return self.word_embeddings.weight
raise Exception(
'word_embeddings_weight() should be called for first and last stage only'
)
def initialize_word_embeddings(self, init_method_normal):
args = get_args()
if not self.share_word_embeddings:
raise Exception(
'initialize_word_embeddings() was called but share_word_embeddings is false'
)
if args.pipeline_model_parallel_size == 1:
return
if mpu.is_pipeline_last_stage():
assert not mpu.is_pipeline_first_stage()
self._word_embeddings_for_head_key = 'word_embeddings_for_head'
self.word_embeddings = mpu.VocabParallelEmbedding(args.
padded_vocab_size, args.hidden_size, init_method=
init_method_normal(args.init_method_std))
self.word_embeddings.weight.data.fill_(0)
self.word_embeddings.weight.shared = True
if torch.distributed.is_initialized():
if mpu.is_pipeline_first_stage() or mpu.is_pipeline_last_stage():
torch.distributed.all_reduce(self.word_embeddings_weight().
data, group=mpu.get_embedding_group())
else:
None
class VitMlpHeadNew(MegatronModule):
"""Pooler layer.
Pool hidden states of a specific token (for example start of the
sequence) and add a linear transformation followed by a tanh.
Arguments:
hidden_size: hidden size
init_method: weight initialization method for the linear layer.
bias is set to zero.
"""
def __init__(self, hidden_size, num_classes):
super(VitMlpHeadNew, self).__init__()
self.dense_in = torch.nn.Linear(hidden_size, hidden_size)
self.dense_out = torch.nn.Linear(hidden_size, num_classes)
torch.nn.init.constant_(self.dense_out.bias, -10)
def forward(self, input_0):
primals_2 = self.dense_in.weight
primals_3 = self.dense_in.bias
primals_4 = self.dense_out.weight
primals_5 = self.dense_out.bias
primals_1 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
ExaSearch/Megatron-DeepSpeed
|
VitMlpHead
| false
| 13,663
|
[
"MIT"
] | 71
|
215dcf9fd4d18d9efa1d15d06c3eb85572957bf3
|
https://github.com/ExaSearch/Megatron-DeepSpeed/tree/215dcf9fd4d18d9efa1d15d06c3eb85572957bf3
|
CapOnlyContrastiveLoss
|
import torch
import torch.nn as nn
import torch.nn.init
def cosine_sim(im, s):
"""Cosine similarity between all the image and sentence pairs
"""
return im.mm(s.t())
def order_sim(im, s):
"""Order embeddings similarity measure $max(0, s-im)$
"""
YmX = s.unsqueeze(1).expand(s.size(0), im.size(0), s.size(1)
) - im.unsqueeze(0).expand(s.size(0), im.size(0), s.size(1))
score = -YmX.clamp(min=0).pow(2).sum(2).sqrt().t()
return score
class CapOnlyContrastiveLoss(nn.Module):
"""
Compute contrastive loss
"""
def __init__(self, margin=0, measure=False, max_violation=False):
super(CapOnlyContrastiveLoss, self).__init__()
self.margin = margin
if measure == 'order':
self.sim = order_sim
else:
self.sim = cosine_sim
self.max_violation = max_violation
def forward(self, im, s, ex_s):
scores = self.sim(im, ex_s)
scores_orig = self.sim(im, s)
diagonal = scores_orig.diag().contiguous().view(im.size(0), 1)
d1 = diagonal.expand_as(scores)
cost_s = (self.margin + scores - d1).clamp(min=0)
if self.max_violation:
cost_s = cost_s.max(1)[0]
return cost_s.sum()
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
def get_inputs():
return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
import torch.nn as nn
import torch.nn.init
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_per_fused_add_clamp_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 16
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r2 = rindex
r1 = rindex // 4
tmp0 = tl.load(in_ptr0 + r2, None)
tmp3 = tl.load(in_ptr1 + 5 * r1, None, eviction_policy='evict_last')
tmp1 = 0.0
tmp2 = tmp0 + tmp1
tmp4 = tmp2 - tmp3
tmp5 = triton_helpers.maximum(tmp4, tmp1)
tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
tmp8 = tl.sum(tmp6, 1)[:, None]
tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4), (4, 1))
assert_size_stride(arg1_1, (4, 4), (4, 1))
assert_size_stride(arg2_1, (4, 4), (4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg0_1, reinterpret_tensor(arg1_1, (4, 4), (1, 4),
0), out=buf0)
del arg1_1
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
extern_kernels.mm(arg0_1, reinterpret_tensor(arg2_1, (4, 4), (1, 4),
0), out=buf1)
del arg0_1
del arg2_1
buf2 = empty_strided_cuda((), (), torch.float32)
get_raw_stream(0)
triton_per_fused_add_clamp_sub_sum_0[grid(1)](buf0, buf1, buf2, 1,
16, XBLOCK=1, num_warps=2, num_stages=1)
del buf0
del buf1
return buf2,
def cosine_sim(im, s):
"""Cosine similarity between all the image and sentence pairs
"""
return im.mm(s.t())
def order_sim(im, s):
"""Order embeddings similarity measure $max(0, s-im)$
"""
YmX = s.unsqueeze(1).expand(s.size(0), im.size(0), s.size(1)
) - im.unsqueeze(0).expand(s.size(0), im.size(0), s.size(1))
score = -YmX.clamp(min=0).pow(2).sum(2).sqrt().t()
return score
class CapOnlyContrastiveLossNew(nn.Module):
"""
Compute contrastive loss
"""
def __init__(self, margin=0, measure=False, max_violation=False):
super(CapOnlyContrastiveLossNew, self).__init__()
self.margin = margin
if measure == 'order':
self.sim = order_sim
else:
self.sim = cosine_sim
self.max_violation = max_violation
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
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]
|
ExplorerFreda/VSE-C
|
CapOnlyContrastiveLoss
| false
| 13,664
|
[
"MIT"
] | 61
|
52d7742adfe017eacd74f36a5953ea2ace9f5fce
|
https://github.com/ExplorerFreda/VSE-C/tree/52d7742adfe017eacd74f36a5953ea2ace9f5fce
|
NormedLinear
|
import torch
import torch.nn.functional as F
import torch.nn as nn
class NormedLinear(nn.Linear):
"""Normalized Linear Layer.
Args:
tempeature (float, optional): Tempeature term. Default to 20.
power (int, optional): Power term. Default to 1.0.
eps (float, optional): The minimal value of divisor to
keep numerical stability. Default to 1e-6.
"""
def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, **kwargs):
super(NormedLinear, self).__init__(*args, **kwargs)
self.tempearture = tempearture
self.power = power
self.eps = eps
self.init_weights()
def init_weights(self):
nn.init.normal_(self.weight, mean=0, std=0.01)
if self.bias is not None:
nn.init.constant_(self.bias, 0)
def forward(self, x):
weight_ = self.weight / (self.weight.norm(dim=1, keepdim=True).pow(
self.power) + self.eps)
x_ = x / (x.norm(dim=1, keepdim=True).pow(self.power) + self.eps)
x_ = x_ * self.tempearture
return F.linear(x_, weight_, self.bias)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'in_features': 4, 'out_features': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_add_div_linalg_vector_norm_mul_pow_0(in_ptr0, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 16
x2 = xindex // 64
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tmp1 * tmp1
tmp4 = tmp3 * tmp3
tmp5 = tmp2 + tmp4
tmp7 = tmp6 * tmp6
tmp8 = tmp5 + tmp7
tmp10 = tmp9 * tmp9
tmp11 = tmp8 + tmp10
tmp12 = libdevice.sqrt(tmp11)
tmp13 = 1e-06
tmp14 = tmp12 + tmp13
tmp15 = tmp0 / tmp14
tmp16 = 20.0
tmp17 = tmp15 * tmp16
tl.store(out_ptr0 + x3, tmp17, xmask)
@triton.jit
def triton_poi_fused_add_div_linalg_vector_norm_pow_1(in_ptr0, out_ptr0,
xnumel, XBLOCK: tl.constexpr):
xnumel = 16
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex
x1 = xindex // 4
tmp0 = tl.load(in_ptr0 + x2, xmask)
tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
tmp2 = tmp1 * tmp1
tmp4 = tmp3 * tmp3
tmp5 = tmp2 + tmp4
tmp7 = tmp6 * tmp6
tmp8 = tmp5 + tmp7
tmp10 = tmp9 * tmp9
tmp11 = tmp8 + tmp10
tmp12 = libdevice.sqrt(tmp11)
tmp13 = 1e-06
tmp14 = tmp12 + tmp13
tmp15 = tmp0 / tmp14
tl.store(out_ptr0 + x2, tmp15, xmask)
def call(args):
primals_1, primals_2, primals_3 = args
args.clear()
assert_size_stride(primals_1, (4, 4), (4, 1))
assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(primals_3, (4,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_div_linalg_vector_norm_mul_pow_0[grid(256)](
primals_2, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
del primals_2
buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
triton_poi_fused_add_div_linalg_vector_norm_pow_1[grid(16)](primals_1,
buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (64, 4), (
4, 1), 0), reinterpret_tensor(buf1, (4, 4), (1, 4), 0), alpha=1,
beta=1, out=buf2)
del buf1
del primals_3
return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0
), primals_1, reinterpret_tensor(buf0, (64, 4), (4, 1), 0)
class NormedLinearNew(nn.Linear):
"""Normalized Linear Layer.
Args:
tempeature (float, optional): Tempeature term. Default to 20.
power (int, optional): Power term. Default to 1.0.
eps (float, optional): The minimal value of divisor to
keep numerical stability. Default to 1e-6.
"""
def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, **kwargs):
super(NormedLinearNew, self).__init__(*args, **kwargs)
self.tempearture = tempearture
self.power = power
self.eps = eps
self.init_weights()
def init_weights(self):
nn.init.normal_(self.weight, mean=0, std=0.01)
if self.bias is not None:
nn.init.constant_(self.bias, 0)
def forward(self, input_0):
primals_1 = self.weight
primals_3 = self.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
FMsunyh/mmdetection
|
NormedLinear
| false
| 13,665
|
[
"Apache-2.0"
] | 240
|
d3683eb06d1041aa3d55f35ad81d8c37718a4c2d
|
https://github.com/FMsunyh/mmdetection/tree/d3683eb06d1041aa3d55f35ad81d8c37718a4c2d
|
Policy
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
self.affine1 = nn.Linear(4, 128)
self.affine2 = nn.Linear(128, 2)
self.saved_log_probs = []
self.rewards = []
def forward(self, x):
x = F.relu(self.affine1(x))
action_scores = self.affine2(x)
return F.softmax(action_scores, dim=1)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime 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):
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
tl.full([XBLOCK], True, tl.int1)
x2 = xindex
x0 = xindex % 128
tmp0 = tl.load(in_out_ptr0 + x2, None)
tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
tmp2 = tmp0 + tmp1
tmp3 = tl.full([1], 0, tl.int32)
tmp4 = triton_helpers.maximum(tmp3, tmp2)
tmp5 = 0.0
tmp6 = tmp4 <= tmp5
tl.store(in_out_ptr0 + x2, tmp4, None)
tl.store(out_ptr0 + x2, tmp6, None)
@triton.jit
def triton_poi_fused__softmax_1(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
x3 = xindex
x0 = xindex % 8
x2 = xindex // 32
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (8 + x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (16 + x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (24 + x0 + 32 * 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 = 128
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x3 = xindex
x0 = xindex % 8
x2 = xindex // 32
tmp0 = tl.load(in_ptr0 + x3, xmask)
tmp1 = tl.load(in_ptr0 + (x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp2 = tl.load(in_ptr0 + (8 + x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp4 = tl.load(in_ptr0 + (16 + x0 + 32 * x2), xmask, eviction_policy=
'evict_last')
tmp6 = tl.load(in_ptr0 + (24 + x0 + 32 * 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 = 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, (2, 128), (128, 1))
assert_size_stride(primals_5, (2,), (1,))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((64, 128), (128, 1), torch.float32)
extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
reinterpret_tensor(primals_1, (4, 128), (1, 4), 0), out=buf0)
del primals_1
buf1 = reinterpret_tensor(buf0, (4, 4, 4, 128), (2048, 512, 128, 1), 0)
del buf0
buf5 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1),
torch.bool)
get_raw_stream(0)
triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf1,
primals_2, buf5, 8192, XBLOCK=256, num_warps=4, num_stages=1)
del primals_2
buf2 = empty_strided_cuda((64, 2), (2, 1), torch.float32)
extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 128),
(128, 1), 0), reinterpret_tensor(primals_4, (128, 2), (1, 128),
0), alpha=1, beta=1, out=buf2)
del primals_5
buf3 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.float32)
triton_poi_fused__softmax_1[grid(128)](buf2, buf3, 128, XBLOCK=128,
num_warps=4, num_stages=1)
buf4 = reinterpret_tensor(buf2, (4, 4, 4, 2), (32, 8, 2, 1), 0)
del buf2
triton_poi_fused__softmax_2[grid(128)](buf3, buf4, 128, XBLOCK=128,
num_warps=4, num_stages=1)
del buf3
return buf4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
), reinterpret_tensor(buf1, (64, 128), (128, 1), 0
), buf4, primals_4, buf5
class PolicyNew(nn.Module):
def __init__(self):
super(PolicyNew, self).__init__()
self.affine1 = nn.Linear(4, 128)
self.affine2 = nn.Linear(128, 2)
self.saved_log_probs = []
self.rewards = []
def forward(self, input_0):
primals_1 = self.affine1.weight
primals_2 = self.affine1.bias
primals_4 = self.affine2.weight
primals_5 = self.affine2.bias
primals_3 = input_0
output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
return output[0]
|
Eunjnnn/ignite
|
Policy
| false
| 13,666
|
[
"BSD-3-Clause"
] | 4,119
|
743089705b2b252aa5e2a0f310da3a8724d6711e
|
https://github.com/Eunjnnn/ignite/tree/743089705b2b252aa5e2a0f310da3a8724d6711e
|
Return
|
import torch
import numpy as np
class Return(torch.nn.Module):
def __init__(self, discount_factor):
super().__init__()
assert 0 <= discount_factor < 1
self.coefficient = 1 / (1 - discount_factor)
self.min_reward = np.float32(-1)
self.max_reward = np.float32(1)
self._low = torch.nn.Parameter(torch.as_tensor(self.coefficient *
self.min_reward, dtype=torch.float32), requires_grad=False)
self._high = torch.nn.Parameter(torch.as_tensor(self.coefficient *
self.max_reward, dtype=torch.float32), requires_grad=False)
def forward(self, val):
val = torch.sigmoid(val)
return self._low + val * (self._high - self._low)
def record(self, values):
for val in values:
if val < self.min_reward:
self.min_reward = np.float32(val)
elif val > self.max_reward:
self.max_reward = np.float32(val)
def update(self):
self._update(self.min_reward, self.max_reward)
def _update(self, min_reward, max_reward):
self._low.data.copy_(torch.as_tensor(self.coefficient * min_reward,
dtype=torch.float32))
self._high.data.copy_(torch.as_tensor(self.coefficient * max_reward,
dtype=torch.float32))
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'discount_factor': 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 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_add_mul_sigmoid_sub_0(in_ptr0, in_ptr1, in_ptr2,
out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 256
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex
tmp0 = tl.load(in_ptr0 + 0)
tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
tmp2 = tl.load(in_ptr1 + x0, xmask)
tmp4 = tl.load(in_ptr2 + 0)
tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
tmp3 = tl.sigmoid(tmp2)
tmp6 = tmp5 - tmp1
tmp7 = tmp3 * tmp6
tmp8 = tmp1 + tmp7
tl.store(out_ptr0 + x0, tmp8, xmask)
def call(args):
arg0_1, arg1_1, arg2_1 = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
assert_size_stride(arg1_1, (), ())
assert_size_stride(arg2_1, (), ())
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_add_mul_sigmoid_sub_0[grid(256)](arg1_1, arg0_1,
arg2_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
del arg0_1
del arg1_1
del arg2_1
return buf0,
class ReturnNew(torch.nn.Module):
def __init__(self, discount_factor):
super().__init__()
assert 0 <= discount_factor < 1
self.coefficient = 1 / (1 - discount_factor)
self.min_reward = np.float32(-1)
self.max_reward = np.float32(1)
self._low = torch.nn.Parameter(torch.as_tensor(self.coefficient *
self.min_reward, dtype=torch.float32), requires_grad=False)
self._high = torch.nn.Parameter(torch.as_tensor(self.coefficient *
self.max_reward, dtype=torch.float32), requires_grad=False)
def record(self, values):
for val in values:
if val < self.min_reward:
self.min_reward = np.float32(val)
elif val > self.max_reward:
self.max_reward = np.float32(val)
def update(self):
self._update(self.min_reward, self.max_reward)
def _update(self, min_reward, max_reward):
self._low.data.copy_(torch.as_tensor(self.coefficient * min_reward,
dtype=torch.float32))
self._high.data.copy_(torch.as_tensor(self.coefficient * max_reward,
dtype=torch.float32))
def forward(self, input_0):
arg1_1 = self._low
arg2_1 = self._high
arg0_1 = input_0
output = call([arg0_1, arg1_1, arg2_1])
return output[0]
|
Eyalcohenx/tonic
|
Return
| false
| 13,667
|
[
"MIT"
] | 350
|
afc15c6fa23fed4f696f68f0acf961964b0172dc
|
https://github.com/Eyalcohenx/tonic/tree/afc15c6fa23fed4f696f68f0acf961964b0172dc
|
SegmentationLoss
|
import torch
import torch.nn as nn
import torch.nn.functional as F
class LovaszHingeLoss(nn.Module):
"""
This class implements the lovasz hinge loss which is the continuous of the IoU for binary segmentation.
Source: https://github.com/bermanmaxim/LovaszSoftmax
"""
def __init__(self) ->None:
"""
Constructor method
"""
super(LovaszHingeLoss, self).__init__()
def _calc_grad(self, label_sorted: 'torch.Tensor') ->torch.Tensor:
"""
Method computes the gradients of the sorted and flattened label
:param label_sorted: (torch.Tensor) Sorted and flattened label of shape [n]
:return: (torch.Tensor) Gradient tensor
"""
label_sum = label_sorted.sum()
intersection = label_sum - label_sorted.cumsum(dim=0)
union = label_sum + (1 - label_sorted).cumsum(dim=0)
iou = 1.0 - intersection / union
iou[1:] = iou[1:] - iou[0:-1]
return iou
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the dice loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Dice loss value
"""
prediction = prediction.flatten(start_dim=0)
label = label.flatten(start_dim=0)
signs = 2.0 * label - 1.0
error = 1.0 - prediction * signs
errors_sorted, permutation = torch.sort(error, dim=0, descending=True)
label_sorted = label[permutation]
grad = self._calc_grad(label_sorted)
loss = torch.dot(F.relu(errors_sorted), grad)
return loss
class DiceLoss(nn.Module):
"""
This class implements the dice loss for multiple instances
"""
def __init__(self, smooth_factor: 'float'=1.0) ->None:
super(DiceLoss, self).__init__()
self.smooth_factor = smooth_factor
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return '{}, smooth factor={}'.format(self.__class__.__name__, self.
smooth_factor)
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the dice loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Dice loss value
"""
prediction = prediction.flatten(start_dim=0)
label = label.flatten(start_dim=0)
loss = torch.tensor(1.0, dtype=torch.float32, device=prediction.device
) - (2.0 * torch.sum(torch.mul(prediction, label)) + self.
smooth_factor) / (torch.sum(prediction) + torch.sum(label) +
self.smooth_factor)
return loss
class FocalLoss(nn.Module):
"""
This class implements the segmentation focal loss.
https://arxiv.org/abs/1708.02002
"""
def __init__(self, alpha: 'float'=0.25, gamma: 'float'=2.0) ->None:
"""
Constructor method
:param alpha: (float) Alpha constant
:param gamma: (float) Gamma constant (see paper)
"""
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return '{}, alpha={}, gamma={}'.format(self.__class__.__name__,
self.alpha, self.gamma)
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the binary cross entropy loss of segmentation masks
:param prediction: (torch.Tensor) Prediction probability
:param label: (torch.Tensor) Label one-hot encoded
:return: (torch.Tensor) Loss value
"""
binary_cross_entropy_loss = -(label * torch.log(prediction.clamp(
min=1e-12)) + (1.0 - label) * torch.log((1.0 - prediction).
clamp(min=1e-12)))
focal_factor = prediction * label + (1.0 - prediction) * (1.0 - label)
loss = ((1.0 - focal_factor) ** self.gamma *
binary_cross_entropy_loss * self.alpha).sum(dim=1).mean()
return loss
class SegmentationLoss(nn.Module):
"""
This class implement the segmentation loss.
"""
def __init__(self, dice_loss: 'nn.Module'=DiceLoss(), focal_loss:
'nn.Module'=FocalLoss(), lovasz_hinge_loss: 'nn.Module'=
LovaszHingeLoss(), w_dice: 'float'=1.0, w_focal: 'float'=0.2,
w_lovasz_hinge: 'float'=0.0) ->None:
super(SegmentationLoss, self).__init__()
self.dice_loss = dice_loss
self.focal_loss = focal_loss
self.lovasz_hinge_loss = lovasz_hinge_loss
self.w_dice = w_dice
self.w_focal = w_focal
self.w_lovasz_hinge = w_lovasz_hinge
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return ('{}, {}, w_focal={}, {}, w_dice={}, {}, w_lovasz_hinge={}'.
format(self.__class__.__name__, self.dice_loss.__class__.
__name__, self.w_dice, self.focal_loss.__class__.__name__, self
.w_focal, self.lovasz_hinge_loss.__class__.__name__, self.
w_lovasz_hinge))
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the segmentation loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Loss value
"""
return self.w_dice * self.dice_loss(prediction, label
) + self.w_focal * self.focal_loss(prediction, label
) + self.w_lovasz_hinge * self.lovasz_hinge_loss(prediction, label)
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_helper_fn_add0(arg0_0, arg1_0):
tmp0 = arg0_0 + arg1_0
return tmp0
@triton.jit
def triton_per_fused_cumsum_index_mul_rsub_sort_sub_sum_0(in_ptr0, in_ptr1,
out_ptr0, out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, out_ptr7,
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 = 2.0
tmp3 = tmp1 * tmp2
tmp4 = 1.0
tmp5 = tmp3 - tmp4
tmp6 = tmp0 * tmp5
tmp7 = tmp4 - tmp6
tmp8 = r0
tmp9 = tmp8.to(tl.int16)
tmp10 = tl.broadcast_to(tmp7, [RBLOCK])
tmp11 = tl.broadcast_to(tmp9, [RBLOCK])
tmp12, tmp13 = triton_helpers.sort_with_index(tmp10, tmp11, None, 0,
stable=False, descending=True)
tmp14 = tmp0 * tmp1
tmp15 = tl.broadcast_to(tmp14, [RBLOCK])
tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
tmp18 = tl.broadcast_to(tmp0, [RBLOCK])
tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
tmp21 = tl.broadcast_to(tmp1, [RBLOCK])
tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
tmp24 = tmp13.to(tl.int64)
tmp25 = tl.full([RBLOCK], 256, tl.int32)
tmp26 = tmp24 + tmp25
tmp27 = tmp24 < 0
tmp28 = tl.where(tmp27, tmp26, tmp24)
tl.device_assert((0 <= tmp28) & (tmp28 < 256),
'index out of bounds: 0 <= tmp28 < 256')
tmp30 = tl.load(in_ptr1 + tmp28, None, eviction_policy='evict_last')
tmp31 = tl.broadcast_to(tmp30, [RBLOCK])
tmp33 = triton_helpers.promote_to_tensor(tl.sum(tmp31, 0))
tmp34 = tmp30.to(tl.float32)
tmp35 = tl.broadcast_to(tmp34, [RBLOCK])
tmp36, = tl.associative_scan((tmp35,), 0, _triton_helper_fn_add0)
tmp37 = tmp4 - tmp30
tmp38 = tmp37.to(tl.float32)
tmp39 = tl.broadcast_to(tmp38, [RBLOCK])
tmp40, = tl.associative_scan((tmp39,), 0, _triton_helper_fn_add0)
tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp12, None)
tl.store(out_ptr6 + tl.broadcast_to(r0, [RBLOCK]), tmp36, None)
tl.store(out_ptr7 + tl.broadcast_to(r0, [RBLOCK]), tmp40, None)
tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp17, None)
tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp20, None)
tl.store(out_ptr4 + tl.full([1], 0, tl.int32), tmp23, None)
tl.store(out_ptr5 + tl.full([1], 0, tl.int32), tmp33, None)
@triton.jit
def triton_per_fused_add_clamp_log_mean_mul_neg_pow_rsub_sum_1(in_ptr0,
in_ptr1, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
RBLOCK: tl.constexpr = 64
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
r0 = rindex % 16
r1 = rindex // 16
tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
tmp22 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
tmp23 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
tmp42 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
tmp43 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
tmp62 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
tmp63 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
tmp2 = tmp0 * tmp1
tmp3 = 1.0
tmp4 = tmp3 - tmp0
tmp5 = tmp3 - tmp1
tmp6 = tmp4 * tmp5
tmp7 = tmp2 + tmp6
tmp8 = tmp3 - tmp7
tmp9 = tmp8 * tmp8
tmp10 = 1e-12
tmp11 = triton_helpers.maximum(tmp0, tmp10)
tmp12 = tl_math.log(tmp11)
tmp13 = tmp1 * tmp12
tmp14 = triton_helpers.maximum(tmp4, tmp10)
tmp15 = tl_math.log(tmp14)
tmp16 = tmp5 * tmp15
tmp17 = tmp13 + tmp16
tmp18 = -tmp17
tmp19 = tmp9 * tmp18
tmp20 = 0.25
tmp21 = tmp19 * tmp20
tmp24 = tmp22 * tmp23
tmp25 = tmp3 - tmp22
tmp26 = tmp3 - tmp23
tmp27 = tmp25 * tmp26
tmp28 = tmp24 + tmp27
tmp29 = tmp3 - tmp28
tmp30 = tmp29 * tmp29
tmp31 = triton_helpers.maximum(tmp22, tmp10)
tmp32 = tl_math.log(tmp31)
tmp33 = tmp23 * tmp32
tmp34 = triton_helpers.maximum(tmp25, tmp10)
tmp35 = tl_math.log(tmp34)
tmp36 = tmp26 * tmp35
tmp37 = tmp33 + tmp36
tmp38 = -tmp37
tmp39 = tmp30 * tmp38
tmp40 = tmp39 * tmp20
tmp41 = tmp21 + tmp40
tmp44 = tmp42 * tmp43
tmp45 = tmp3 - tmp42
tmp46 = tmp3 - tmp43
tmp47 = tmp45 * tmp46
tmp48 = tmp44 + tmp47
tmp49 = tmp3 - tmp48
tmp50 = tmp49 * tmp49
tmp51 = triton_helpers.maximum(tmp42, tmp10)
tmp52 = tl_math.log(tmp51)
tmp53 = tmp43 * tmp52
tmp54 = triton_helpers.maximum(tmp45, tmp10)
tmp55 = tl_math.log(tmp54)
tmp56 = tmp46 * tmp55
tmp57 = tmp53 + tmp56
tmp58 = -tmp57
tmp59 = tmp50 * tmp58
tmp60 = tmp59 * tmp20
tmp61 = tmp41 + tmp60
tmp64 = tmp62 * tmp63
tmp65 = tmp3 - tmp62
tmp66 = tmp3 - tmp63
tmp67 = tmp65 * tmp66
tmp68 = tmp64 + tmp67
tmp69 = tmp3 - tmp68
tmp70 = tmp69 * tmp69
tmp71 = triton_helpers.maximum(tmp62, tmp10)
tmp72 = tl_math.log(tmp71)
tmp73 = tmp63 * tmp72
tmp74 = triton_helpers.maximum(tmp65, tmp10)
tmp75 = tl_math.log(tmp74)
tmp76 = tmp66 * tmp75
tmp77 = tmp73 + tmp76
tmp78 = -tmp77
tmp79 = tmp70 * tmp78
tmp80 = tmp79 * tmp20
tmp81 = tmp61 + tmp80
tmp82 = tl.broadcast_to(tmp81, [XBLOCK, RBLOCK])
tmp84 = tl.sum(tmp82, 1)[:, None]
tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp84, None)
@triton.jit
def triton_per_fused_add_div_dot_lift_fresh_mean_mul_relu_rsub_sub_2(
in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5,
in_ptr6, xnumel, rnumel):
XBLOCK: tl.constexpr = 1
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
tl.full([1], xoffset, tl.int32)
tl.full([RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[:]
tl.full([RBLOCK], True, tl.int1)
r0 = rindex
tmp0 = tl.load(in_ptr0 + r0, None)
tmp6 = tl.load(in_out_ptr0 + 0)
tmp7 = tl.broadcast_to(tmp6, [RBLOCK])
tmp24 = tl.load(in_ptr1 + r0, None)
tmp26 = tl.load(in_ptr2 + r0, None)
tmp35 = tl.load(in_ptr3 + 0)
tmp36 = tl.broadcast_to(tmp35, [1])
tmp40 = tl.load(in_ptr4 + 0)
tmp41 = tl.broadcast_to(tmp40, [1])
tmp42 = tl.load(in_ptr5 + 0)
tmp43 = tl.broadcast_to(tmp42, [1])
tmp49 = tl.load(in_ptr6 + 0)
tmp50 = tl.broadcast_to(tmp49, [1])
tmp1 = tl.full([1], 0, tl.int32)
tmp2 = triton_helpers.maximum(tmp1, tmp0)
tmp3 = r0
tmp4 = tl.full([1], 1, tl.int64)
tmp5 = tmp3 >= tmp4
tmp8 = tl.load(in_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp5, other=0.0)
tmp9 = tmp7 - tmp8
tmp10 = tl.load(in_ptr2 + tl.broadcast_to(r0, [RBLOCK]), tmp5, other=0.0)
tmp11 = tmp7 + tmp10
tmp12 = tmp9 / tmp11
tmp13 = 1.0
tmp14 = tmp13 - tmp12
tmp15 = tl.load(in_ptr1 + tl.broadcast_to(-1 + r0, [RBLOCK]), tmp5,
other=0.0)
tmp16 = tmp7 - tmp15
tmp17 = tl.load(in_ptr2 + tl.broadcast_to(-1 + r0, [RBLOCK]), tmp5,
other=0.0)
tmp18 = tmp7 + tmp17
tmp19 = tmp16 / tmp18
tmp20 = tmp13 - tmp19
tmp21 = tmp14 - tmp20
tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
tmp23 = tl.where(tmp5, tmp21, tmp22)
tmp25 = tmp7 - tmp24
tmp27 = tmp7 + tmp26
tmp28 = tmp25 / tmp27
tmp29 = tmp13 - tmp28
tmp30 = tl.where(tmp5, tmp23, tmp29)
tmp31 = tmp2 * tmp30
tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
tmp37 = 2.0
tmp38 = tmp36 * tmp37
tmp39 = tmp38 + tmp13
tmp44 = tmp41 + tmp43
tmp45 = tmp44 + tmp13
tmp46 = tmp39 / tmp45
tmp47 = tmp13 - tmp46
tmp48 = tmp47 * tmp13
tmp51 = 64.0
tmp52 = tmp50 / tmp51
tmp53 = 0.2
tmp54 = tmp52 * tmp53
tmp55 = tmp48 + tmp54
tmp56 = 0.0
tmp57 = tmp34 * tmp56
tmp58 = tmp55 + tmp57
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp58, 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((256,), (1,), torch.float32)
buf5 = empty_strided_cuda((), (), torch.float32)
buf6 = empty_strided_cuda((), (), torch.float32)
buf7 = empty_strided_cuda((), (), torch.float32)
buf2 = empty_strided_cuda((), (), torch.float32)
buf3 = empty_strided_cuda((256,), (1,), torch.float32)
buf4 = empty_strided_cuda((256,), (1,), torch.float32)
get_raw_stream(0)
triton_per_fused_cumsum_index_mul_rsub_sort_sub_sum_0[grid(1)](arg0_1,
arg1_1, buf0, buf5, buf6, buf7, buf2, buf3, buf4, 1, 256,
num_warps=2, num_stages=1)
buf9 = empty_strided_cuda((), (), torch.float32)
triton_per_fused_add_clamp_log_mean_mul_neg_pow_rsub_sum_1[grid(1)](
arg0_1, arg1_1, buf9, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
del arg1_1
buf10 = buf2
del buf2
buf11 = buf10
del buf10
triton_per_fused_add_div_dot_lift_fresh_mean_mul_relu_rsub_sub_2[grid
(1)](buf11, buf0, buf3, buf4, buf5, buf6, buf7, buf9, 1, 256,
num_warps=2, num_stages=1)
del buf0
del buf3
del buf4
del buf5
del buf6
del buf7
del buf9
return buf11,
class LovaszHingeLoss(nn.Module):
"""
This class implements the lovasz hinge loss which is the continuous of the IoU for binary segmentation.
Source: https://github.com/bermanmaxim/LovaszSoftmax
"""
def __init__(self) ->None:
"""
Constructor method
"""
super(LovaszHingeLoss, self).__init__()
def _calc_grad(self, label_sorted: 'torch.Tensor') ->torch.Tensor:
"""
Method computes the gradients of the sorted and flattened label
:param label_sorted: (torch.Tensor) Sorted and flattened label of shape [n]
:return: (torch.Tensor) Gradient tensor
"""
label_sum = label_sorted.sum()
intersection = label_sum - label_sorted.cumsum(dim=0)
union = label_sum + (1 - label_sorted).cumsum(dim=0)
iou = 1.0 - intersection / union
iou[1:] = iou[1:] - iou[0:-1]
return iou
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the dice loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Dice loss value
"""
prediction = prediction.flatten(start_dim=0)
label = label.flatten(start_dim=0)
signs = 2.0 * label - 1.0
error = 1.0 - prediction * signs
errors_sorted, permutation = torch.sort(error, dim=0, descending=True)
label_sorted = label[permutation]
grad = self._calc_grad(label_sorted)
loss = torch.dot(F.relu(errors_sorted), grad)
return loss
class DiceLoss(nn.Module):
"""
This class implements the dice loss for multiple instances
"""
def __init__(self, smooth_factor: 'float'=1.0) ->None:
super(DiceLoss, self).__init__()
self.smooth_factor = smooth_factor
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return '{}, smooth factor={}'.format(self.__class__.__name__, self.
smooth_factor)
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the dice loss
:param prediction: (torch.Tensor) Prediction
:param label: (torch.Tensor) Label
:return: (torch.Tensor) Dice loss value
"""
prediction = prediction.flatten(start_dim=0)
label = label.flatten(start_dim=0)
loss = torch.tensor(1.0, dtype=torch.float32, device=prediction.device
) - (2.0 * torch.sum(torch.mul(prediction, label)) + self.
smooth_factor) / (torch.sum(prediction) + torch.sum(label) +
self.smooth_factor)
return loss
class FocalLoss(nn.Module):
"""
This class implements the segmentation focal loss.
https://arxiv.org/abs/1708.02002
"""
def __init__(self, alpha: 'float'=0.25, gamma: 'float'=2.0) ->None:
"""
Constructor method
:param alpha: (float) Alpha constant
:param gamma: (float) Gamma constant (see paper)
"""
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return '{}, alpha={}, gamma={}'.format(self.__class__.__name__,
self.alpha, self.gamma)
def forward(self, prediction: 'torch.Tensor', label: 'torch.Tensor'
) ->torch.Tensor:
"""
Forward pass computes the binary cross entropy loss of segmentation masks
:param prediction: (torch.Tensor) Prediction probability
:param label: (torch.Tensor) Label one-hot encoded
:return: (torch.Tensor) Loss value
"""
binary_cross_entropy_loss = -(label * torch.log(prediction.clamp(
min=1e-12)) + (1.0 - label) * torch.log((1.0 - prediction).
clamp(min=1e-12)))
focal_factor = prediction * label + (1.0 - prediction) * (1.0 - label)
loss = ((1.0 - focal_factor) ** self.gamma *
binary_cross_entropy_loss * self.alpha).sum(dim=1).mean()
return loss
class SegmentationLossNew(nn.Module):
"""
This class implement the segmentation loss.
"""
def __init__(self, dice_loss: 'nn.Module'=DiceLoss(), focal_loss:
'nn.Module'=FocalLoss(), lovasz_hinge_loss: 'nn.Module'=
LovaszHingeLoss(), w_dice: 'float'=1.0, w_focal: 'float'=0.2,
w_lovasz_hinge: 'float'=0.0) ->None:
super(SegmentationLossNew, self).__init__()
self.dice_loss = dice_loss
self.focal_loss = focal_loss
self.lovasz_hinge_loss = lovasz_hinge_loss
self.w_dice = w_dice
self.w_focal = w_focal
self.w_lovasz_hinge = w_lovasz_hinge
def __repr__(self):
"""
Get representation of the loss module
:return: (str) String including information
"""
return ('{}, {}, w_focal={}, {}, w_dice={}, {}, w_lovasz_hinge={}'.
format(self.__class__.__name__, self.dice_loss.__class__.
__name__, self.w_dice, self.focal_loss.__class__.__name__, self
.w_focal, self.lovasz_hinge_loss.__class__.__name__, self.
w_lovasz_hinge))
def forward(self, input_0, input_1):
arg0_1 = input_0
arg1_1 = input_1
output = call([arg0_1, arg1_1])
return output[0]
|
ChristophReich1996/Cell-DETR
|
SegmentationLoss
| false
| 13,668
|
[
"MIT"
] | 55
|
4d0c3a2d3ffd19184c8443e5b3a6dccc053c77ea
|
https://github.com/ChristophReich1996/Cell-DETR/tree/4d0c3a2d3ffd19184c8443e5b3a6dccc053c77ea
|
SobLoss
|
import torch
class SobLoss(torch.nn.Module):
"""
Sobolev norm penalty on function
(sum |x_{i} - x{i+1}|^p)^{1/p}
parameters:
p - dimension of norm
"""
def __init__(self, p):
super(SobLoss, self).__init__()
self.p = p
def forward(self, beta):
hdiff = beta[1:] - beta[:-1]
return torch.norm(hdiff, p=self.p)
def get_inputs():
return [torch.rand([4, 4, 4, 4])]
def get_init_inputs():
return [[], {'p': 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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
@triton.jit
def triton_per_fused_linalg_vector_norm_sub_0(in_out_ptr0, in_ptr0, xnumel,
rnumel, XBLOCK: tl.constexpr):
rnumel = 192
RBLOCK: tl.constexpr = 256
xoffset = tl.program_id(0) * XBLOCK
xoffset + tl.arange(0, XBLOCK)[:, None]
tl.full([XBLOCK, RBLOCK], True, tl.int1)
rindex = tl.arange(0, RBLOCK)[None, :]
rmask = rindex < rnumel
r0 = rindex
tmp0 = tl.load(in_ptr0 + (64 + r0), rmask, other=0.0)
tmp1 = tl.load(in_ptr0 + r0, rmask, other=0.0)
tmp2 = tmp0 - tmp1
tmp3 = tmp2 * tmp2
tmp4 = tmp3 * tmp3
tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
tmp7 = tl.where(rmask, tmp5, 0)
tmp8 = tl.sum(tmp7, 1)[:, None]
tmp9 = 0.25
tmp10 = libdevice.pow(tmp8, tmp9)
tl.debug_barrier()
tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp10, None)
def call(args):
arg0_1, = args
args.clear()
assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((), (), torch.float32)
buf1 = buf0
del buf0
get_raw_stream(0)
triton_per_fused_linalg_vector_norm_sub_0[grid(1)](buf1, arg0_1, 1,
192, XBLOCK=1, num_warps=2, num_stages=1)
del arg0_1
return buf1,
class SobLossNew(torch.nn.Module):
"""
Sobolev norm penalty on function
(sum |x_{i} - x{i+1}|^p)^{1/p}
parameters:
p - dimension of norm
"""
def __init__(self, p):
super(SobLossNew, self).__init__()
self.p = p
def forward(self, input_0):
arg0_1 = input_0
output = call([arg0_1])
return output[0]
|
Filco306/TopologyLayer
|
SobLoss
| false
| 13,669
|
[
"MIT"
] | 250
|
1d6261017a80cff0ee06bb896ded40777b0989b4
|
https://github.com/Filco306/TopologyLayer/tree/1d6261017a80cff0ee06bb896ded40777b0989b4
|
ShiftedConv
|
import math
import torch
import torch.nn as nn
from numpy import prod
def getLayerNormalizationFactor(x):
"""
Get He's constant for the given layer
https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf
"""
size = x.weight.size()
fan_in = prod(size[1:])
return math.sqrt(2.0 / fan_in)
class ConstrainedLayer(nn.Module):
"""
A handy refactor that allows the user to:
- initialize one layer's bias to zero
- apply He's initialization at runtime
"""
def __init__(self, module, equalized=True, lrMul=1.0, initBiasToZero=True):
"""
equalized (bool): if true, the layer's weight should evolve within
the range (-1, 1)
initBiasToZero (bool): if true, bias will be initialized to zero
"""
super(ConstrainedLayer, self).__init__()
self.module = module
self.equalized = equalized
if initBiasToZero and module.bias is not None:
self.module.bias.data.fill_(0)
if self.equalized:
self.module.weight.data.normal_(0, 1)
self.weight = getLayerNormalizationFactor(self.module) * lrMul
def forward(self, x):
x = self.module(x)
if self.equalized:
x *= self.weight
return x
class EqualizedConv1d(ConstrainedLayer):
def __init__(self, nChannelsPrevious, nChannels, kernelSize, padding=0,
bias=True, stride=1, **kwargs):
"""
A nn.Conv2d module with specific constraints
Args:
nChannelsPrevious (int): number of channels in the previous layer
nChannels (int): number of channels of the current layer
kernelSize (int): size of the convolutional kernel
padding (int): convolution's padding
bias (bool): with bias ?
"""
ConstrainedLayer.__init__(self, nn.Conv1d(nChannelsPrevious,
nChannels, kernelSize, padding=padding, bias=bias, stride=
stride), **kwargs)
class ShiftedConv(nn.Module):
def __init__(self, dimOutputAR, dimOutputEncoder, kernelSize):
super(ShiftedConv, self).__init__()
self.module = EqualizedConv1d(dimOutputAR, dimOutputEncoder,
kernelSize, equalized=True, padding=0)
self.kernelSize = kernelSize
def forward(self, x):
N, _S, C = x.size()
x = x.permute(0, 2, 1)
padding = torch.zeros(N, C, self.kernelSize - 1, device=x.device)
x = torch.cat([padding, x], dim=2)
x = self.module(x)
x = x.permute(0, 2, 1)
return x
def get_inputs():
return [torch.rand([4, 4, 4])]
def get_init_inputs():
return [[], {'dimOutputAR': 4, 'dimOutputEncoder': 4, 'kernelSize': 4}]
|
import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
import math
import torch.nn as nn
from numpy import prod
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
xnumel = 112
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x0 = xindex % 7
x1 = xindex // 7 % 4
x2 = xindex // 28
x3 = xindex
tmp0 = x0
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 3, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = 0.0
tmp6 = tl.full(tmp5.shape, 0.0, tmp5.dtype)
tmp7 = tl.where(tmp4, tmp5, tmp6)
tmp8 = tmp0 >= tmp3
tl.full([1], 7, tl.int64)
tmp11 = tl.load(in_ptr0 + (x1 + 4 * (-3 + x0) + 16 * x2), tmp8 & xmask,
eviction_policy='evict_last', other=0.0)
tmp12 = tl.where(tmp4, tmp7, tmp11)
tl.store(out_ptr0 + x3, tmp12, xmask)
@triton.jit
def triton_poi_fused_convolution_mul_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
tmp3 = 0.3535533905932738
tmp4 = tmp2 * tmp3
tl.store(in_out_ptr0 + x3, 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), (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, 7), (28, 7, 1), torch.float32)
get_raw_stream(0)
triton_poi_fused_cat_0[grid(112)](primals_1, buf0, 112, XBLOCK=128,
num_warps=4, num_stages=1)
del primals_1
buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,),
padding=(0,), dilation=(1,), transposed=False, output_padding=(
0,), groups=1, bias=None)
assert_size_stride(buf1, (4, 4, 4), (16, 4, 1))
buf2 = buf1
del buf1
triton_poi_fused_convolution_mul_1[grid(64)](buf2, primals_3, 64,
XBLOCK=64, num_warps=1, num_stages=1)
del primals_3
return reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0), primals_2, buf0
def getLayerNormalizationFactor(x):
"""
Get He's constant for the given layer
https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf
"""
size = x.weight.size()
fan_in = prod(size[1:])
return math.sqrt(2.0 / fan_in)
class ConstrainedLayer(nn.Module):
"""
A handy refactor that allows the user to:
- initialize one layer's bias to zero
- apply He's initialization at runtime
"""
def __init__(self, module, equalized=True, lrMul=1.0, initBiasToZero=True):
"""
equalized (bool): if true, the layer's weight should evolve within
the range (-1, 1)
initBiasToZero (bool): if true, bias will be initialized to zero
"""
super(ConstrainedLayer, self).__init__()
self.module = module
self.equalized = equalized
if initBiasToZero and module.bias is not None:
self.module.bias.data.fill_(0)
if self.equalized:
self.module.weight.data.normal_(0, 1)
self.weight = getLayerNormalizationFactor(self.module) * lrMul
def forward(self, x):
x = self.module(x)
if self.equalized:
x *= self.weight
return x
class EqualizedConv1d(ConstrainedLayer):
def __init__(self, nChannelsPrevious, nChannels, kernelSize, padding=0,
bias=True, stride=1, **kwargs):
"""
A nn.Conv2d module with specific constraints
Args:
nChannelsPrevious (int): number of channels in the previous layer
nChannels (int): number of channels of the current layer
kernelSize (int): size of the convolutional kernel
padding (int): convolution's padding
bias (bool): with bias ?
"""
ConstrainedLayer.__init__(self, nn.Conv1d(nChannelsPrevious,
nChannels, kernelSize, padding=padding, bias=bias, stride=
stride), **kwargs)
class ShiftedConvNew(nn.Module):
def __init__(self, dimOutputAR, dimOutputEncoder, kernelSize):
super(ShiftedConvNew, self).__init__()
self.module = EqualizedConv1d(dimOutputAR, dimOutputEncoder,
kernelSize, equalized=True, padding=0)
self.kernelSize = kernelSize
def forward(self, input_0):
primals_1 = self.module.module.weight
primals_3 = self.module.module.bias
primals_2 = input_0
output = call([primals_1, primals_2, primals_3])
return output[0]
|
EyalSel/CPC_audio
|
ShiftedConv
| false
| 13,670
|
[
"MIT"
] | 260
|
b98a1bdf1fe9ea219816db7a6c28115d404a3510
|
https://github.com/EyalSel/CPC_audio/tree/b98a1bdf1fe9ea219816db7a6c28115d404a3510
|
LogisticCumulativeLink
|
import torch
from torch import nn
class LogisticCumulativeLink(nn.Module):
"""
Converts a single number to the proportional odds of belonging to a class.
Parameters
----------
num_classes : int
Number of ordered classes to partition the odds into.
init_cutpoints : str (default='ordered')
How to initialize the cutpoints of the model. Valid values are
- ordered : cutpoints are initialized to halfway between each class.
- random : cutpoints are initialized with random values.
"""
def __init__(self, num_classes: 'int', init_cutpoints: 'str'='ordered'
) ->None:
assert num_classes > 2, 'Only use this model if you have 3 or more classes'
super().__init__()
self.num_classes = num_classes
self.init_cutpoints = init_cutpoints
if init_cutpoints == 'ordered':
num_cutpoints = self.num_classes - 1
cutpoints = torch.arange(num_cutpoints).float() - num_cutpoints / 2
self.cutpoints = nn.Parameter(cutpoints)
elif init_cutpoints == 'random':
cutpoints = torch.rand(self.num_classes - 1).sort()[0]
self.cutpoints = nn.Parameter(cutpoints)
else:
raise ValueError(
f'{init_cutpoints} is not a valid init_cutpoints type')
def forward(self, X: 'torch.Tensor') ->torch.Tensor:
"""
Equation (11) from
"On the consistency of ordinal regression methods", Pedregosa et. al.
"""
sigmoids = torch.sigmoid(self.cutpoints - X)
link_mat = sigmoids[:, 1:] - sigmoids[:, :-1]
link_mat = torch.cat((sigmoids[:, [0]], link_mat, 1 - sigmoids[:, [
-1]]), dim=1)
return link_mat
def get_inputs():
return [torch.rand([4, 4, 4, 3])]
def get_init_inputs():
return [[], {'num_classes': 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_lift_fresh_0(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.full([1], 0, tl.int64)
tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp0, None)
@triton.jit
def triton_poi_fused_lift_fresh_1(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.full([1], -1, tl.int64)
tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp0, None)
@triton.jit
def triton_poi_fused_cat_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
constexpr):
xnumel = 240
xoffset = tl.program_id(0) * XBLOCK
xindex = xoffset + tl.arange(0, XBLOCK)[:]
xmask = xindex < xnumel
x2 = xindex // 12 % 5
x0 = xindex % 3
x3 = xindex // 60
x4 = xindex % 12
x5 = xindex
tmp0 = x2
tl.full([1], 0, tl.int64)
tmp3 = tl.full([1], 1, tl.int64)
tmp4 = tmp0 < tmp3
tmp5 = tl.load(in_ptr0 + x0, tmp4 & xmask, eviction_policy='evict_last',
other=0.0)
tmp6 = tl.load(in_ptr1 + (x4 + 48 * x3), tmp4 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp7 = tmp5 - tmp6
tmp8 = tl.sigmoid(tmp7)
tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
tmp10 = tl.where(tmp4, tmp8, tmp9)
tmp11 = tmp0 >= tmp3
tmp12 = tl.full([1], 4, tl.int64)
tmp13 = tmp0 < tmp12
tmp14 = tmp11 & tmp13
tmp15 = tl.load(in_ptr0 + x0, tmp14 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp16 = tl.load(in_ptr1 + (12 + x4 + 12 * (-1 + x2) + 48 * x3), tmp14 &
xmask, other=0.0)
tmp17 = tmp15 - tmp16
tmp18 = tl.sigmoid(tmp17)
tmp19 = tl.load(in_ptr1 + (x4 + 12 * (-1 + x2) + 48 * x3), tmp14 &
xmask, other=0.0)
tmp20 = tmp15 - tmp19
tmp21 = tl.sigmoid(tmp20)
tmp22 = tmp18 - tmp21
tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype)
tmp24 = tl.where(tmp14, tmp22, tmp23)
tmp25 = tmp0 >= tmp12
tl.full([1], 5, tl.int64)
tmp28 = tl.load(in_ptr0 + x0, tmp25 & xmask, eviction_policy=
'evict_last', other=0.0)
tmp29 = tl.load(in_ptr1 + (36 + x4 + 48 * x3), tmp25 & xmask,
eviction_policy='evict_last', other=0.0)
tmp30 = tmp28 - tmp29
tmp31 = tl.sigmoid(tmp30)
tmp32 = 1.0
tmp33 = tmp32 - tmp31
tmp34 = tl.full(tmp33.shape, 0.0, tmp33.dtype)
tmp35 = tl.where(tmp25, tmp33, tmp34)
tmp36 = tl.where(tmp14, tmp24, tmp35)
tmp37 = tl.where(tmp4, tmp10, tmp36)
tl.store(out_ptr0 + x5, tmp37, xmask)
def call(args):
primals_1, primals_2 = args
args.clear()
assert_size_stride(primals_1, (3,), (1,))
assert_size_stride(primals_2, (4, 4, 4, 3), (48, 12, 3, 1))
with torch.cuda._DeviceGuard(0):
torch.cuda.set_device(0)
buf0 = empty_strided_cuda((1,), (1,), torch.int64)
get_raw_stream(0)
triton_poi_fused_lift_fresh_0[grid(1)](buf0, 1, XBLOCK=1, num_warps
=1, num_stages=1)
buf1 = empty_strided_cuda((1,), (1,), torch.int64)
triton_poi_fused_lift_fresh_1[grid(1)](buf1, 1, XBLOCK=1, num_warps
=1, num_stages=1)
buf2 = empty_strided_cuda((4, 5, 4, 3), (60, 12, 3, 1), torch.float32)
triton_poi_fused_cat_2[grid(240)](primals_1, primals_2, buf2, 240,
XBLOCK=256, num_warps=4, num_stages=1)
return buf2, primals_1, primals_2, buf0, buf1
class LogisticCumulativeLinkNew(nn.Module):
"""
Converts a single number to the proportional odds of belonging to a class.
Parameters
----------
num_classes : int
Number of ordered classes to partition the odds into.
init_cutpoints : str (default='ordered')
How to initialize the cutpoints of the model. Valid values are
- ordered : cutpoints are initialized to halfway between each class.
- random : cutpoints are initialized with random values.
"""
def __init__(self, num_classes: 'int', init_cutpoints: 'str'='ordered'
) ->None:
assert num_classes > 2, 'Only use this model if you have 3 or more classes'
super().__init__()
self.num_classes = num_classes
self.init_cutpoints = init_cutpoints
if init_cutpoints == 'ordered':
num_cutpoints = self.num_classes - 1
cutpoints = torch.arange(num_cutpoints).float() - num_cutpoints / 2
self.cutpoints = nn.Parameter(cutpoints)
elif init_cutpoints == 'random':
cutpoints = torch.rand(self.num_classes - 1).sort()[0]
self.cutpoints = nn.Parameter(cutpoints)
else:
raise ValueError(
f'{init_cutpoints} is not a valid init_cutpoints type')
def forward(self, input_0):
primals_1 = self.cutpoints
primals_2 = input_0
output = call([primals_1, primals_2])
return output[0]
|
EthanRosenthal/medallion
|
LogisticCumulativeLink
| false
| 13,671
|
[
"MIT"
] | 74
|
063fe875f5122063e6f616512cffd9ffa4df1974
|
https://github.com/EthanRosenthal/medallion/tree/063fe875f5122063e6f616512cffd9ffa4df1974
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.