Skip to content

add aten.sum.default #2807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
15 changes: 11 additions & 4 deletions backends/vulkan/runtime/graph/ops/impl/Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,17 @@ void add_sum_dim_IntList(
const auto& dims_to_sum = graph.get_val(opt_dim).toIntList();
int64_t in_dim = in_tensor.sizes().size();

for (const auto& dim : dims_to_sum) {
// Normalize (negative) dim into range [0, self.dim() - 1]
int64_t dim_normalized = normalize(dim, in_dim);
dims_set.insert(dim_normalized);
if (dims_to_sum.empty()) {
// If dim is not specified, reduce over all dims
for (int64_t i = 0; i < in_dim; ++i) {
dims_set.insert(i);
}
} else {
for (const auto& dim : dims_to_sum) {
// Normalize (negative) dim into range [0, self.dim() - 1]
int64_t dim_normalized = normalize(dim, in_dim);
dims_set.insert(dim_normalized);
}
}

// Reduce the higher dimensionalities first, otherwise when keepdim is
Expand Down
10 changes: 8 additions & 2 deletions backends/vulkan/serialization/vulkan_graph_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ def create_tensor_value(self, spec: TensorSpec, constant_id: int = -1) -> int:

def create_scalar_list_value(self, arg: List[_ScalarType]) -> int:
new_id = len(self.values)
if isinstance(arg[0], bool):
if len(arg) == 0:
self.values.append(
vk_graph_schema.VkValue(vk_graph_schema.IntList(items=[]))
)
elif isinstance(arg[0], bool):
self.values.append(
vk_graph_schema.VkValue(
vk_graph_schema.BoolList(items=[cast(bool, e) for e in arg])
Expand Down Expand Up @@ -227,7 +231,9 @@ def get_or_create_value_for(self, arg: _Argument):
return self.create_scalar_value(arg)
elif isinstance(arg, TensorSpec):
return self.create_tensor_value(arg)
elif isinstance(arg, list) and isinstance(arg[0], _ScalarType):
elif isinstance(arg, list) and (
len(arg) == 0 or isinstance(arg[0], _ScalarType)
):
# pyre-ignore[6]
return self.create_scalar_list_value(arg)
elif isinstance(arg, list) and isinstance(arg[0], Node):
Expand Down
19 changes: 19 additions & 0 deletions backends/vulkan/test/test_vulkan_delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,25 @@ def forward(self, x):
memory_layouts=[vk_graph_schema.VkMemoryLayout.TENSOR_CHANNELS_PACKED],
)

def test_vulkan_backend_sum(self):
class SumModule(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, x):
x = torch.sum(x, (), keepdim=True)
x = torch.sum(x)
return x

module = SumModule()
sample_inputs = (torch.rand(size=(3, 2, 7, 5), dtype=torch.float32),)

self.lower_module_and_test_output(
module,
sample_inputs,
memory_layouts=[vk_graph_schema.VkMemoryLayout.TENSOR_CHANNELS_PACKED],
)

def test_vulkan_backend_conv2d(self):
class Conv2dModule(torch.nn.Module):
def __init__(self):
Expand Down