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: 5 additions & 0 deletions ggml/src/ggml-hrx/dispatch/dispatch-scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ static bool node_inputs_are_available(const Graph & graph,
const GraphNode & node,
const std::vector<bool> & covered_nodes) {
for (const ValueId input : node.inputs) {
// CPY writes into its destination tensor, which the importer also records as its output.
// An empty copy does not need that destination to have been produced already.
if (node.op == GGML_OP_CPY && input == node.output) {
continue;
}
if (!value_is_available(graph, input, covered_nodes)) {
return false;
}
Expand Down
40 changes: 40 additions & 0 deletions ggml/src/ggml-hrx/dispatch_registration/common/dispatch-copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace {

static constexpr uint64_t kMaximumCopyElements = uint64_t{ 1 } << 30;
static constexpr KernelCatalogRef kCopyF32Kernel = GGML_HRX_KERNEL_REF("loom_libs", "ggml_copy_f32");
static constexpr KernelCatalogRef kCopyF32F16Kernel = GGML_HRX_KERNEL_REF("loom_libs", "ggml_copy_f32_f16");
static constexpr KernelCatalogRef kCopyStridedSourceF32Kernel =
GGML_HRX_KERNEL_REF("loom_libs", "ggml_copy_strided_source_f32");
static constexpr KernelCatalogRef kConcatDim0F32Kernel = GGML_HRX_KERNEL_REF("loom_libs", "ggml_concat_dim0_f32");
Expand Down Expand Up @@ -122,6 +123,37 @@ static bool match_copy_f32_dispatch(const DispatchMatchContext & context, Dispat
return true;
}

static bool match_copy_f32_f16_dispatch(const DispatchMatchContext & context, DispatchMatch & match) {
const GraphNode * node = context.root_node;
if (node == nullptr || node->op != GGML_OP_CPY || node->inputs.size() != 2) {
return false;
}

const Value * source = graph_value(context.graph, node->inputs[0]);
const Value * target = graph_value(context.graph, node->inputs[1]);
const Value * output = graph_value(context.graph, node->output);
if (source == nullptr || target == nullptr || output == nullptr || source->type != GGML_TYPE_F32 ||
target->type != GGML_TYPE_F16 || output->type != GGML_TYPE_F16 || !source->contiguous ||
!target->contiguous || !output->contiguous || source->ne != output->ne || target->ne != output->ne ||
source->element_count <= 0 || source->element_count != output->element_count ||
source->element_count % 4 != 0 || static_cast<uint64_t>(source->element_count) > kMaximumCopyElements ||
source->byte_count != static_cast<size_t>(source->element_count) * sizeof(float) ||
output->byte_count != static_cast<size_t>(output->element_count) * sizeof(ggml_fp16_t) ||
target->storage != output->storage || source->storage == output->storage) {
return false;
}

Dispatch dispatch;
dispatch.kernel = make_kernel_specialization(kCopyF32F16Kernel);
dispatch.kernel.workload_specialization = WorkloadSpecialization::Dynamic;
dispatch.kernel.integer_parameters.emplace("element_count", source->element_count);
dispatch.bindings.push_back({ source->id, 0, source->byte_count });
dispatch.bindings.push_back({ output->id, 0, output->byte_count });
match.covered_nodes.push_back(context.root_index);
match.dispatches.push_back(std::move(dispatch));
return true;
}

static bool match_cont_or_dup_f32_dispatch(const DispatchMatchContext & context, DispatchMatch & match) {
const GraphNode * node = context.root_node;
if (node == nullptr) {
Expand Down Expand Up @@ -210,6 +242,14 @@ static bool match_concat_dim0_f32_dispatch(const DispatchMatchContext & context,
} // namespace

void register_copy_dispatch(DispatchRegistryBuilder & registry) {
registry.add({
"common.copy_f32_f16",
GGML_OP_CPY,
DispatchMatchKind::SingleOp,
0,
DispatchSource::Common,
match_copy_f32_f16_dispatch,
});
registry.add({
"common.copy_f32",
GGML_OP_CPY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static bool has_flash_attention_params(const GraphNode & node) {
return false;
}
return nearly_equal(params->max_bias, 0.0f) && nearly_equal(params->logit_softcap, 0.0f) &&
(params->prec == GGML_PREC_DEFAULT || params->prec == GGML_PREC_F32);
(params->prec == GGML_PREC_UNDEFINED || params->prec == GGML_PREC_F32);
}

static std::string to_config_value(int64_t value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../qwen/dispatch-llm-profiles.h"
#include "dispatch-activation-publication.h"
#include "dispatch-layout-utils.h"
#include "dispatch-mul-mat-common.h"
#include "dispatch-mul-mat-weight-format.h"
#include "ggml.h"
Expand All @@ -19,6 +20,9 @@ namespace ggml::hrx {
namespace {

static constexpr KernelCatalogRef kGetRowsF32Kernel = GGML_HRX_KERNEL_REF("loom_libs", "ggml_get_rows_f32");
static constexpr KernelCatalogRef kGetRowsI32Kernel = GGML_HRX_KERNEL_REF("loom_libs", "ggml_get_rows_i32");
static constexpr KernelCatalogRef kGetRowsStridedF32Kernel =
GGML_HRX_KERNEL_REF("loom_libs", "ggml_get_rows_strided_f32");
static constexpr KernelCatalogRef kGetRowsScalar3dF32Kernel =
GGML_HRX_KERNEL_REF("loom_libs", "ggml_get_rows_scalar_3d_f32");
static constexpr KernelCatalogRef kRepeatScalar3dF32Kernel =
Expand Down Expand Up @@ -465,6 +469,81 @@ static bool match_get_rows_f32_dispatch(const DispatchMatchContext & context, Di
return true;
}

static bool match_get_rows_i32_dispatch(const DispatchMatchContext & context, DispatchMatch & match) {
const GraphNode * node = context.root_node;
if (node == nullptr || node->op != GGML_OP_GET_ROWS || node->inputs.size() != 2) {
return false;
}

const Value * weight = graph_value(context.graph, node->inputs[0]);
const Value * ids = graph_value(context.graph, node->inputs[1]);
const Value * output = graph_value(context.graph, node->output);
if (weight == nullptr || ids == nullptr || output == nullptr || weight->type != GGML_TYPE_I32 ||
ids->type != GGML_TYPE_I32 || output->type != GGML_TYPE_I32 || !weight->contiguous ||
!ids->contiguous || !output->contiguous || !is_2d(*weight) || !is_1d_or_2d_column(*ids) ||
!is_2d(*output) || output->ne[0] != weight->ne[0] || output->ne[1] != ids->ne[0] ||
weight->ne[0] > 32768 || !is_supported_row_count(weight->ne[1]) ||
!is_supported_token_count(ids->ne[0]) || weight->storage_root == output->storage_root) {
return false;
}

Dispatch dispatch;
dispatch.kernel = make_kernel_specialization(kGetRowsI32Kernel);
dispatch.kernel.workload_specialization = WorkloadSpecialization::Dynamic;
dispatch.kernel.integer_parameters.emplace("token_count", ids->ne[0]);
dispatch.kernel.integer_parameters.emplace("row_count", weight->ne[1]);
dispatch.kernel.integer_parameters.emplace("hidden_size", weight->ne[0]);
dispatch.bindings.push_back({ ids->id, 0, ids->byte_count });
dispatch.bindings.push_back({ weight->id, 0, weight->byte_count });
dispatch.bindings.push_back({ output->id, 0, output->byte_count });
match.covered_nodes.push_back(context.root_index);
match.dispatches.push_back(std::move(dispatch));
return true;
}

static bool match_get_rows_strided_f32_dispatch(const DispatchMatchContext & context, DispatchMatch & match) {
const GraphNode * node = context.root_node;
if (node == nullptr || node->op != GGML_OP_GET_ROWS || node->inputs.size() != 2) {
return false;
}

const Value * source = graph_value(context.graph, node->inputs[0]);
const Value * ids = graph_value(context.graph, node->inputs[1]);
const Value * output = graph_value(context.graph, node->output);
if (source == nullptr || ids == nullptr || output == nullptr || source->type != GGML_TYPE_F32 ||
ids->type != GGML_TYPE_I32 || output->type != GGML_TYPE_F32 || source->contiguous ||
!ids->contiguous || !output->contiguous || !is_2d(*source) || !is_1d_or_2d_column(*ids) ||
!is_2d(*output) || source->nb[0] != sizeof(float) || source->nb[1] < sizeof(float) ||
source->nb[1] % sizeof(float) != 0 ||
output->ne[0] != source->ne[0] || output->ne[1] != ids->ne[0] || source->ne[0] > 32768 ||
!is_supported_row_count(source->ne[1]) || !is_supported_token_count(ids->ne[0]) ||
source->storage_root == output->storage_root || ids->storage_root == output->storage_root) {
return false;
}

size_t source_span = 0;
if (!strided_f32_storage_span_bytes(*source, source_span) ||
source_span / sizeof(float) > static_cast<size_t>(kMaximumHiddenElements) ||
source->nb[1] / sizeof(float) > static_cast<size_t>(kMaximumHiddenElements)) {
return false;
}

Dispatch dispatch;
dispatch.kernel = make_kernel_specialization(kGetRowsStridedF32Kernel);
dispatch.kernel.workload_specialization = WorkloadSpecialization::Dynamic;
dispatch.kernel.integer_parameters.emplace("token_count", ids->ne[0]);
dispatch.kernel.integer_parameters.emplace("row_count", source->ne[1]);
dispatch.kernel.integer_parameters.emplace("hidden_size", source->ne[0]);
dispatch.kernel.integer_parameters.emplace("row_stride", source->nb[1] / sizeof(float));
dispatch.kernel.integer_parameters.emplace("source_span", source_span / sizeof(float));
dispatch.bindings.push_back({ ids->id, 0, ids->byte_count });
dispatch.bindings.push_back({ source->storage_root, source->storage_offset, source_span });
dispatch.bindings.push_back({ output->id, 0, output->byte_count });
match.covered_nodes.push_back(context.root_index);
match.dispatches.push_back(std::move(dispatch));
return true;
}

static bool match_get_rows_scale_f32_dispatch(const DispatchMatchContext & context,
DispatchMatch & dispatch_match) {
const GetRowsScaleMatch match = match_get_rows_scale_f32(context);
Expand Down Expand Up @@ -597,6 +676,22 @@ static bool match_get_rows_rmsnorm_binary_dispatch(const DispatchMatchContext &
} // namespace

void register_get_rows_dispatches(DispatchRegistryBuilder & registry) {
registry.add({
"common.get_rows.strided_f32",
GGML_OP_GET_ROWS,
DispatchMatchKind::SingleOp,
100,
DispatchSource::Common,
match_get_rows_strided_f32_dispatch,
});
registry.add({
"common.get_rows.i32",
GGML_OP_GET_ROWS,
DispatchMatchKind::SingleOp,
100,
DispatchSource::Common,
match_get_rows_i32_dispatch,
});
registry.add({
"common.repeat.scalar_3d_f32",
GGML_OP_REPEAT,
Expand Down
3 changes: 0 additions & 3 deletions ggml/src/ggml-hrx/ggml-hrx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,6 @@ static bool zero_output_elision_safe_op(enum ggml_op op) {
case GGML_OP_DUP:
case GGML_OP_ADD:
case GGML_OP_ADD_ID:
case GGML_OP_ADD1:
case GGML_OP_SUB:
case GGML_OP_MUL:
case GGML_OP_DIV:
Expand Down Expand Up @@ -805,8 +804,6 @@ static bool zero_output_elision_safe_op(enum ggml_op op) {
case GGML_OP_GET_ROWS:
case GGML_OP_GET_ROWS_BACK:
case GGML_OP_DIAG:
case GGML_OP_DIAG_MASK_INF:
case GGML_OP_DIAG_MASK_ZERO:
case GGML_OP_SOFT_MAX:
case GGML_OP_SOFT_MAX_BACK:
case GGML_OP_ROPE:
Expand Down
2 changes: 1 addition & 1 deletion ggml/src/ggml-hrx/graph/graph-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ OpParams parse_op_params(const json & item) {
item.value("scale", 0.0f),
item.value("max_bias", 0.0f),
item.value("logit_softcap", 0.0f),
static_cast<ggml_prec>(item.value("prec", static_cast<int>(GGML_PREC_DEFAULT))),
static_cast<ggml_prec>(item.value("prec", static_cast<int>(GGML_PREC_UNDEFINED))),
};
}
if (kind == "soft_max") {
Expand Down
2 changes: 1 addition & 1 deletion ggml/src/ggml-hrx/graph/op-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct FlashAttnExtParams {
float scale = 0.0f;
float max_bias = 0.0f;
float logit_softcap = 0.0f;
ggml_prec prec = GGML_PREC_DEFAULT;
ggml_prec prec = GGML_PREC_UNDEFINED;
};

struct SoftMaxParams {
Expand Down
128 changes: 128 additions & 0 deletions ggml/src/ggml-hrx/kernel-corpus/kernels/loom-libs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@
{
"path": "ops/rows/get_rows_f32.loom"
},
{
"path": "ops/rows/get_rows_i32.loom"
},
{
"path": "ops/rows/get_rows_strided_f32.loom"
},
{
"path": "ops/rows/get_rows_scalar_3d_f32.loom"
},
Expand Down Expand Up @@ -6965,6 +6971,128 @@
},
"compile_dependencies": []
},
{
"name": "ggml_get_rows_i32",
"family": "loom_libs",
"symbol": "ggml_get_rows_i32",
"source": "ops/rows/get_rows_i32.loom",
"workload_parameters": [
{
"name": "token_count",
"type": "index"
},
{
"name": "row_count",
"type": "index"
},
{
"name": "hidden_size",
"type": "index"
}
],
"launch_parameters": [
{
"name": "token_count",
"type": "index"
},
{
"name": "row_count",
"type": "index"
},
{
"name": "hidden_size",
"type": "index"
}
],
"bindings": [
"token_ids",
"weight",
"output"
],
"binding_access": [
"read",
"read",
"read_write"
],
"target_selector": "",
"compile_recipe": {
"mode": "direct",
"primary_sources": [
"ops/rows/get_rows_i32.loom"
],
"library_sources": []
},
"compile_dependencies": []
},
{
"name": "ggml_get_rows_strided_f32",
"family": "loom_libs",
"symbol": "ggml_get_rows_strided_f32",
"source": "ops/rows/get_rows_strided_f32.loom",
"workload_parameters": [
{
"name": "token_count",
"type": "index"
},
{
"name": "row_count",
"type": "index"
},
{
"name": "hidden_size",
"type": "index"
},
{
"name": "row_stride",
"type": "index"
},
{
"name": "source_span",
"type": "index"
}
],
"launch_parameters": [
{
"name": "token_count",
"type": "index"
},
{
"name": "row_count",
"type": "index"
},
{
"name": "hidden_size",
"type": "index"
},
{
"name": "row_stride",
"type": "index"
},
{
"name": "source_span",
"type": "index"
}
],
"bindings": [
"token_ids",
"source",
"output"
],
"binding_access": [
"read",
"read",
"write"
],
"target_selector": "",
"compile_recipe": {
"mode": "direct",
"primary_sources": [
"ops/rows/get_rows_strided_f32.loom"
],
"library_sources": []
},
"compile_dependencies": []
},
{
"name": "ggml_get_rows_f32",
"family": "loom_libs",
Expand Down
Loading
Loading