Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions extension_backends/extension_codegen_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def codegen_loops(self):
code.splice(self.reduction_suffix)
return code

def define_kernel(self, wrapper, src_code, kernel_name):
if src_code in wrapper.src_to_kernel:
kernel_name = wrapper.src_to_kernel[src_code]
else:
wrapper.src_to_kernel[src_code] = kernel_name
wrapper.define_kernel(kernel_name, src_code, cuda=False)

def codegen_kernel(self, wrapper):
arg_defs, call_args, arg_types = self.args.cpp_argdefs()
arg_defs = ",\n".ljust(25).join(arg_defs)
Expand All @@ -115,8 +122,8 @@ def codegen_kernel(self, wrapper):
codecache_def.splice(code)
if not V.graph.cpp_wrapper:
codecache_def.writeline("''')")

wrapper.define_kernel(kernel_name, codecache_def.getvalue(), cuda=False)
self.define_kernel(wrapper, codecache_def.getvalue(), kernel_name)
# generate the code to call this
wrapper.generate_kernel_call(kernel_name, call_args, cuda=False)
print(code.getvalue())
Expand Down Expand Up @@ -180,9 +187,15 @@ def __init__(self, scheduler):
self._scheduling = cpp.CppScheduling(scheduler)

def can_fuse_vertical(self, node1, node2):
return False
return self._can_fuse_horizontal_impl(node1, node2) and not node1.is_reduction()

def can_fuse_horizontal(self, node1, node2):
_, (vars1, reduce1) = node1.group
_, (vars2, reduce2) = node2.group
if vars1 == vars2 and reduce1 == reduce2:
return True
if reduce1 == () and vars1 == vars2 + reduce2:
return True
return False

def group_fn(self, sizes):
Expand All @@ -193,8 +206,9 @@ def codegen_nodes(self, nodes):
nodes, key=lambda x: int(x.is_reduction())
).group

ex_kernel = ExtensionKernel()

for node in nodes:
ex_kernel = ExtensionKernel()
vars, reduction_vars = ex_kernel.set_ranges(group, reduction_group)
with ex_kernel:
node.run(vars, reduction_vars)
Expand Down
64 changes: 49 additions & 15 deletions test_extension_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ def remove_build_path():
shutil.rmtree(default_build_root, ignore_errors=True)


class AddModule(torch.nn.Module):
def __init__(self):
super(AddModule, self).__init__()

def forward(self, a, b):
return a + b

class MultiplyModule(torch.nn.Module):
def __init__(self):
super(MultiplyModule, self).__init__()

def forward(self, a, b):
return a * b

class MultiplyAddModule(torch.nn.Module):
def __init__(self):
super(MultiplyAddModule, self).__init__()
self.add = AddModule()
self.multiply = MultiplyModule()
def forward(self, a, b, c):
tmp = self.multiply(a, b)
return self.add(tmp, c)

class ExtensionBackendTests(TestCase):
module = None

Expand Down Expand Up @@ -114,33 +137,44 @@ def test_open_device_registration(self):

self.assertFalse(self.module.custom_op_called())
device = self.module.custom_device()
x = torch.empty(1000, 1000).to(device=device).fill_(1)
a = torch.empty(1000, 1000).to(device=device).fill_(1)
self.assertTrue(self.module.custom_op_called())
y = torch.empty(1000, 1000).to(device=device).fill_(2)
z = torch.empty(1000).to(device=device).fill_(3)
ref = torch.empty(1000, 1000).fill_(3)

self.assertTrue(x.device == device)
self.assertTrue(y.device == device)
self.assertTrue(z.device == device)

def fn(a, b, c):
return a / b + c
b = torch.empty(1000, 1000).to(device=device).fill_(2)
c = torch.empty(1000, 1000).to(device=device).fill_(3)
d = torch.empty(1000, 1000).to(device=device).fill_(4)
ref = torch.empty(1000, 1000).fill_(8)

self.assertTrue(a.device == device)
self.assertTrue(b.device == device)
self.assertTrue(c.device == device)
self.assertTrue(d.device == device)

def fn(a, b, c, d):
return vectoradd(a, b), vectoradd(c, d)

def vectoradd(a, b):
return a + b

def vectorsub(a, b):
return a - b

def vectormult(a, b):
return a * b

def vectordiv(a, b):
return a / b


def reduce_sum(a):
return torch.sum(a, axis=-1)

metrics.reset()
opt_fn = torch.compile()(reduce_sum)
code = run_and_get_cpp_code(opt_fn, x)
opt_fn = torch.compile()(fn)
code = run_and_get_cpp_code(opt_fn, a, b, c, d)
FileCheck().check("void kernel").check("extension_device").run(
code
)
opt_fn(x)
res = opt_fn(x)
res = opt_fn(a, b, c, d)
self.assertEqual(ref, res.to(device="cpu"))


Expand Down