Skip to content
Merged
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
5 changes: 2 additions & 3 deletions backends/vulkan/custom_ops_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import executorch.backends.vulkan.patterns as vk_patterns
import torch.library

from torch._subclasses.fake_tensor import FakeTensor

namespace = "et_vk"
Expand Down Expand Up @@ -259,7 +258,7 @@ def linear_q4gsw(
weights, [1, group_size], weight_scales, weight_zeros, torch.int8, -8, 7
)

out = torch.nn.functional.linear(x, weights)
out = torch.nn.functional.linear(x, weights, bias)
return out


Expand All @@ -273,7 +272,7 @@ def linear_dq8ca_q4gsw(
group_size: int,
bias: Optional[torch.Tensor] = None,
):
return linear_q4gsw(x, weights, weight_scales, group_size)
return linear_q4gsw(x, weights, weight_scales, group_size, bias)


name = "linear_q4gsw"
Expand Down
69 changes: 54 additions & 15 deletions backends/vulkan/op_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def update_features_impl(op: OpKey):
torch.ops.aten.sym_size.int,
operator.add,
operator.sub,
operator.floordiv,
operator.mul,
operator.lt,
operator.gt,
operator.ge,
Expand Down Expand Up @@ -279,6 +281,26 @@ def register_bitwise_and():
)


@update_features(exir_ops.edge.aten.bitwise_not.default)
def register_bitwise_not():
return OpFeatures(
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.BOOL_T,
supports_resize=True,
supports_highdim=True,
)


@update_features(exir_ops.edge.aten.logical_and.default)
def register_logical_and():
return OpFeatures(
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.BOOL_T,
supports_resize=True,
supports_highdim=True,
)


# =============================================================================
# BinaryScalarOp.cpp
# =============================================================================
Expand All @@ -301,16 +323,22 @@ def register_pow_tensor_scalar():

@update_features(exir_ops.edge.aten._to_copy.default)
def register_to_copy():
def check_to_copy_node(node: torch.fx.Node) -> bool:
# Only single-arg _to_copy is supported
return len(node.args) == 1
def pick_to_copy_storage(
node: torch.fx.Node,
) -> Tuple[utils.TensorRepSet, utils.TensorRepSet]:
in_dtype = node.args[0].meta["val"].dtype # type: ignore[union-attr]
out_dtype = node.meta["val"].dtype
fp_types = {torch.float16, torch.float32}
if in_dtype in fp_types and out_dtype in fp_types:
return utils.ANY_STORAGE, utils.ANY_STORAGE
return utils.CONTIGUOUS_BUFFER, utils.CONTIGUOUS_BUFFER

return OpFeatures(
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_T,
outputs_dtypes=utils.FP_INT_T,
inputs_dtypes=utils.FP_INT_BOOL_T,
outputs_dtypes=utils.FP_INT_BOOL_T,
supports_resize=True,
are_node_inputs_supported_fn=check_to_copy_node,
pick_io_storage_fn=pick_to_copy_storage,
)


Expand All @@ -327,7 +355,7 @@ def check_to_copy_node(node: torch.fx.Node) -> bool:
)
def register_softmax_cpp_ops():
return OpFeatures(
inputs_storage=utils.ANY_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_T,
supports_resize=True,
)
Expand Down Expand Up @@ -705,7 +733,7 @@ def register_reduce_cpp_ops():
)
def register_argreduce_cpp_ops():
return OpFeatures(
inputs_storage=utils.ANY_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_T,
outputs_dtypes=utils.INT_T,
supports_resize=True,
Expand Down Expand Up @@ -1120,7 +1148,7 @@ def register_gather():
@update_features(exir_ops.edge.aten.expand_copy.default)
def register_expand_copy():
return OpFeatures(
inputs_storage=utils.ANY_BUFFER,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_BOOL_T,
supports_resize=False,
supports_highdim=True,
Expand Down Expand Up @@ -1285,7 +1313,7 @@ def check_index_tensor_node(node: torch.fx.Node) -> bool:
@update_features(exir_ops.edge.aten.arange.start_step)
def register_arange():
return OpFeatures(
inputs_storage=utils.CHANNELS_PACKED_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_T,
)

Expand All @@ -1298,8 +1326,9 @@ def register_arange():
@update_features(exir_ops.edge.aten.constant_pad_nd.default)
def register_constant_pad_nd():
return OpFeatures(
inputs_storage=utils.CHANNELS_PACKED_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_BOOL_T,
supports_resize=True,
)


Expand All @@ -1320,7 +1349,7 @@ def register_constant_pad_nd():
)
def register_full_cpp_ops():
return OpFeatures(
inputs_storage=utils.CHANNELS_PACKED_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_BOOL_T,
)

Expand All @@ -1335,6 +1364,7 @@ def register_scalar_tensor():
return OpFeatures(
inputs_storage=utils.CHANNELS_PACKED_TEXTURE,
inputs_dtypes=utils.FP_INT_T,
supports_resize=True,
)


Expand Down Expand Up @@ -1377,7 +1407,7 @@ def register_grid_priors():
@update_features(exir_ops.edge.aten.repeat.default)
def register_repeat():
return OpFeatures(
inputs_storage=utils.ANY_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_INT_BOOL_T,
)

Expand All @@ -1389,11 +1419,20 @@ def register_repeat():

@update_features(exir_ops.edge.aten.embedding.default)
def register_embedding():
def check_embedding_weight_size(node: torch.fx.Node) -> bool:
weight = node.args[0]
if isinstance(weight, torch.fx.Node) and utils.is_tensor_node(weight):
numel = weight.meta["val"].numel()
if numel > utils.DEFAULT_BUFFER_LIMIT:
return False
return True

return OpFeatures(
inputs_storage=utils.CHANNELS_PACKED_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=[utils.FP_T, utils.INT_T],
supports_prepacking=True,
supports_resize=True,
are_node_inputs_supported_fn=check_embedding_weight_size,
)


Expand Down Expand Up @@ -1439,7 +1478,7 @@ def register_native_group_norm():
@update_features(exir_ops.edge.aten.native_layer_norm.default)
def register_native_layer_norm():
return OpFeatures(
inputs_storage=utils.ANY_TEXTURE,
inputs_storage=utils.ANY_STORAGE,
inputs_dtypes=utils.FP_T,
supports_prepacking=True,
supports_resize=True,
Expand Down
26 changes: 15 additions & 11 deletions backends/vulkan/patterns/quantized_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@
# LICENSE file in the root directory of this source tree.

import operator

from typing import Optional

import executorch.backends.vulkan.utils as utils

import torch
import torch.nn.functional as F

from executorch.backends.transforms.utils import (
create_constant_placeholder,
get_param_tensor,
)

from executorch.backends.vulkan.patterns.pattern_registry import (
PatternMatch,
register_pattern_detector,
register_pattern_replacement,
)

from executorch.exir import ExportedProgram
from executorch.exir.dialects._ops import ops as exir_ops

from torch.export.graph_signature import InputKind


Expand Down Expand Up @@ -398,6 +392,12 @@ def make_linear_q4gsw_op(
force_update=True,
)

# Pad bias to multiple of 4 if present
if match.bias_node is not None:
bias_tensor = get_param_tensor(ep, match.bias_node)
if bias_tensor is not None:
utils.align_width_and_update_state_dict(ep, match.bias_node, bias_tensor)

with graph_module.graph.inserting_before(match.output_node):
linear_q4gsw_node = graph_module.graph.create_node(
"call_function",
Expand All @@ -407,6 +407,7 @@ def make_linear_q4gsw_op(
match.weight_node,
match.weight_scales_node,
group_size,
match.bias_node,
),
)

Expand Down Expand Up @@ -445,6 +446,12 @@ def make_linear_dq8ca_q4gsw_op(
force_update=True,
)

# Pad bias to multiple of 4 if present
if match.bias_node is not None:
bias_tensor = get_param_tensor(ep, match.bias_node)
if bias_tensor is not None:
utils.align_width_and_update_state_dict(ep, match.bias_node, bias_tensor)

first_graph_node = list(graph_module.graph.nodes)[0]
with graph_module.graph.inserting_before(first_graph_node):
weight_tensor_name = utils.get_tensor_name(ep, match.weight_node)
Expand Down Expand Up @@ -474,6 +481,7 @@ def make_linear_dq8ca_q4gsw_op(
weight_sums_node,
match.weight_scales_node,
group_size,
match.bias_node,
),
)

Expand Down Expand Up @@ -538,6 +546,7 @@ def make_linear_q8ta_q8csw_custom_op(
match.weight_node,
weight_sums_node,
match.weight_scales_node,
match.bias_node,
),
)

Expand Down Expand Up @@ -637,7 +646,6 @@ def replace_quantized_linear_patterns(
assert weight_zeros_tensor is not None

# Route to appropriate custom op.
# q8ta_linear supports bias, so check it first before the bias guard.
if (
match.is_input_static_per_tensor_quantized()
and match.is_weight_perchannel_quantized()
Expand All @@ -646,10 +654,6 @@ def replace_quantized_linear_patterns(
make_q8ta_linear_custom_op(ep, graph_module, match, weight_tensor)
return

# Remaining ops do not support bias
if match.bias_node is not None:
return

if (
match.is_weight_only_quantized()
and match.is_weight_pergroup_quantized()
Expand Down
39 changes: 0 additions & 39 deletions backends/vulkan/runtime/graph/ops/glsl/arange.glsl

This file was deleted.

38 changes: 38 additions & 0 deletions backends/vulkan/runtime/graph/ops/glsl/arange_buffer.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#version 450 core

${define_required_extensions("buffer", DTYPE)}

#define PRECISION ${PRECISION}

#define T ${buffer_scalar_type(DTYPE)}

${define_active_storage_type("buffer")}

layout(std430) buffer;

#include "indexing.glslh"

${layout_declare_tensor(B, "w", "t_out", DTYPE, "buffer")}

${layout_declare_ubo(B, "BufferMetadata", "outp")}
${layout_declare_ubo(B, "float", "start")}
${layout_declare_ubo(B, "float", "step")}

layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;

void main() {
const uint out_bufi = gl_GlobalInvocationID.x;
if (out_of_bounds(out_bufi, outp)) {
return;
}

t_out[out_bufi] = T(start + out_bufi * step);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

arange:
arange_buffer:
parameter_names_with_default_values:
NDIM: 3
DTYPE: int32
STORAGE: texture3d
PACKING: C_packed
DTYPE: float
STORAGE: buffer
generate_variant_forall:
DTYPE:
- VALUE: half
- VALUE: float
- VALUE: int32
shader_variants:
- NAME: arange
- NAME: arange_buffer
Loading
Loading