Skip to content
Open
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
4 changes: 2 additions & 2 deletions backends/vulkan/custom_ops_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,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 +273,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
8 changes: 3 additions & 5 deletions backends/vulkan/patterns/quantized_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def make_linear_q4gsw_op(
match.weight_node,
match.weight_scales_node,
group_size,
match.bias_node,
),
)

Expand Down Expand Up @@ -459,6 +460,7 @@ def make_linear_dq8ca_q4gsw_op(
weight_sums_node,
match.weight_scales_node,
group_size,
match.bias_node,
),
)

Expand Down Expand Up @@ -523,6 +525,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 @@ -622,7 +625,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 @@ -631,10 +633,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
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,11 @@ void main() {
group_size);
}

if (apply_bias > 0) {
FPPerOutChannelParams bias_tile;
load_bias_tile(bias_tile, n4);
add_bias_to_out_tile(out_tile, bias_tile);
}

write_output_tile_with_checks(out_tile, n4, m, N4, M);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ void apply_weight_scales_and_biases(
}
}

void add_bias_to_out_tile(
inout FPOutTile tile,
const FPPerOutChannelParams bias) {
[[unroll]] for (int m = 0; m < TILE_M; ++m) {
[[unroll]] for (int n4 = 0; n4 < TILE_N4; ++n4) {
tile.data[m][n4] = tile.data[m][n4] + bias.data[n4];
}
}
}

void accumulate_out_tile_with_out_tile(
inout FPOutTile accum,
const FPOutTile other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ void main() {
// Only the first thread will write out result
if (lid == 0) {
out_tile = partial_sums[0];
if (apply_bias > 0) {
FPPerOutChannelParams bias_tile;
load_bias_tile(bias_tile, n4);
add_bias_to_out_tile(out_tile, bias_tile);
}
write_output_tile_with_checks(out_tile, n4, 0, N4, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,11 @@ void main() {
}
}

if (apply_bias > 0) {
FPPerOutChannelParams bias_tile;
load_bias_tile(bias_tile, n4);
add_bias_to_out_tile(out_tile, bias_tile);
}

write_output_tile_with_checks(out_tile, n4, m, N4, M);
}
16 changes: 5 additions & 11 deletions backends/vulkan/test/custom_ops/q4gsw_linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ TestCase create_test_case_from_config(
input_dtype,
storage_type,
utils::kWidthPacked,
DataGenType::ZEROS);
config.has_bias ? DataGenType::RANDOM : DataGenType::ZEROS);
bias.set_constant(true);
if (!config.has_bias) {
bias.set_none(true);
Expand Down Expand Up @@ -237,9 +237,10 @@ std::vector<TestCase> generate_quantized_linear_test_cases() {
{32, 64, 32, 16},
{32, 128, 64, 32},
{32, 256, 128, 64},
// No bias tests
{32, 128, 64, 32, false},
{32, 256, 128, 64, false},
// With bias
{4, 64, 32, 16, true},
{4, 128, 64, 32, true},
{32, 128, 64, 32, true},
// Performance test cases
{1, 2048, 2048, 128},
{128, 2048, 2048, 128},
Expand Down Expand Up @@ -499,13 +500,6 @@ void reference_impl(TestCase& test_case) {
}

int64_t quantized_linear_flop_calculator(const TestCase& test_case) {
int input_idx = 0;
int weight_idx = 1;
if (test_case.operator_name().find("dq8ca") != std::string::npos) {
input_idx = 0;
weight_idx = 3; // Weight comes after input, input_scale, input_zero_point
}

// Get input and weight dimensions
const auto& input_sizes = test_case.inputs()[0].get_tensor_sizes();
const auto& output_sizes = test_case.outputs()[0].get_tensor_sizes();
Expand Down
Loading