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
6 changes: 6 additions & 0 deletions ggml/src/ggml-hrx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ ggml_add_backend_library(ggml-hrx
dispatch/dispatch.h
dispatch/transient-allocator.cpp
dispatch/transient-allocator.h
dispatch_registration/common/dispatch-activation-publication.cpp
dispatch_registration/common/dispatch-activation-publication.h
dispatch_registration/common/dispatch-binary.cpp
dispatch_registration/common/dispatch-binary.h
dispatch_registration/common/dispatch-binary-common.h
Expand Down Expand Up @@ -234,8 +236,12 @@ ggml_add_backend_library(ggml-hrx
dispatch_registration/common/dispatch-scale.h
dispatch_registration/common/dispatch-unary.cpp
dispatch_registration/common/dispatch-unary.h
dispatch_registration/common/iq-codebook-resource.cpp
dispatch_registration/common/iq-codebook-resource.h
dispatch_registration/dispatch-registry.cpp
dispatch_registration/dispatch-registry.h
dispatch_registration/immutable-program-resource.cpp
dispatch_registration/immutable-program-resource.h
dispatch_registration/llm/dispatch-attention-qkv.cpp
dispatch_registration/llm/dispatch-attention-qkv.h
dispatch_registration/llm/dispatch-gated-delta-net.cpp
Expand Down
9 changes: 9 additions & 0 deletions ggml/src/ggml-hrx/dispatch/command-plan-metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static bool moe_routing_bundle_matches(const CommandPlanMoeRoutingBundle & lhs,
void CommandPlanMetadata::clear() {
generated_resources_.clear();
alternate_values_.clear();
activation_publication_diagnostics_.clear();
moe_routing_bundles_.clear();
}

Expand All @@ -51,6 +52,9 @@ bool CommandPlanMetadata::append(CommandPlanMetadata && other, Status & status)
return false;
}
}
for (CommandPlanActivationPublicationDiagnostic & diagnostic : other.activation_publication_diagnostics_) {
append_activation_publication_diagnostic(std::move(diagnostic));
}
for (CommandPlanMoeRoutingBundle & bundle : other.moe_routing_bundles_) {
if (!append_moe_routing_bundle(std::move(bundle), status)) {
return false;
Expand All @@ -59,6 +63,11 @@ bool CommandPlanMetadata::append(CommandPlanMetadata && other, Status & status)
return true;
}

void CommandPlanMetadata::append_activation_publication_diagnostic(
CommandPlanActivationPublicationDiagnostic diagnostic) {
activation_publication_diagnostics_.push_back(std::move(diagnostic));
}

bool CommandPlanMetadata::append_generated_resource(CommandPlanGeneratedResource resource, Status & status) {
for (const CommandPlanGeneratedResource & existing : generated_resources_) {
if (existing.source_value == resource.source_value && existing.role == resource.role) {
Expand Down
19 changes: 19 additions & 0 deletions ggml/src/ggml-hrx/dispatch/command-plan-metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ struct CommandPlanAlternateValue {
std::string name;
};

struct CommandPlanActivationPublicationDiagnostic {
ValueId source_value;
ValueId alternate_value;
ValueId consumer_value;
std::string requested_format;
std::string publication_name;
std::string publication_stage;
std::string fallback_reason;
size_t alias_depth = 0;
bool mixed_fanout = false;
};

struct CommandPlanMoeRoutingBundle {
ValueId route_ids;
ValueId route_weights;
Expand All @@ -109,6 +121,8 @@ class CommandPlanMetadata {

bool append_alternate_value(CommandPlanAlternateValue alternate, Status & status);

void append_activation_publication_diagnostic(CommandPlanActivationPublicationDiagnostic diagnostic);

bool append_moe_routing_bundle(CommandPlanMoeRoutingBundle bundle, Status & status);

const CommandPlanGeneratedResource * find_generated_resource(ValueId source_value,
Expand All @@ -126,11 +140,16 @@ class CommandPlanMetadata {

const std::vector<CommandPlanAlternateValue> & alternate_values() const { return alternate_values_; }

const std::vector<CommandPlanActivationPublicationDiagnostic> & activation_publication_diagnostics() const {
return activation_publication_diagnostics_;
}

const std::vector<CommandPlanMoeRoutingBundle> & moe_routing_bundles() const { return moe_routing_bundles_; }

private:
std::vector<CommandPlanGeneratedResource> generated_resources_;
std::vector<CommandPlanAlternateValue> alternate_values_;
std::vector<CommandPlanActivationPublicationDiagnostic> activation_publication_diagnostics_;
std::vector<CommandPlanMoeRoutingBundle> moe_routing_bundles_;
};

Expand Down
123 changes: 92 additions & 31 deletions ggml/src/ggml-hrx/dispatch/command-plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,40 @@ inline bool same_full_value_range(const Value & lhs, const Value & rhs) {
return lhs.storage == rhs.storage && lhs.storage_offset == rhs.storage_offset && lhs.byte_count == rhs.byte_count;
}

inline const CommandPlanAlternateValue * find_alternate_value(const Graph & graph,
const CommandPlan & plan,
ValueId graph_value,
ggml_type type,
size_t byte_count) {
const CommandPlanAlternateValue * exact = find_alternate_value(plan, graph_value, type, byte_count);
inline bool same_full_ordered_value_range(const Value & lhs, const Value & rhs) {
return same_full_value_range(lhs, rhs) && lhs.type == rhs.type && lhs.element_count == rhs.element_count &&
lhs.contiguous && rhs.contiguous;
}

inline bool same_packed_row_geometry(const Value & lhs, const Value & rhs) {
return same_full_ordered_value_range(lhs, rhs) && lhs.ne[0] > 0 && rhs.ne[0] > 0 && lhs.ne[0] == rhs.ne[0] &&
lhs.element_count / lhs.ne[0] == rhs.element_count / rhs.ne[0];
}

inline bool same_q8_1_x4_geometry(const Value & lhs, const Value & rhs) {
return same_full_ordered_value_range(lhs, rhs) && lhs.ne[0] > 0 && rhs.ne[0] > 0 && lhs.ne[0] % 128 == 0 &&
rhs.ne[0] % 128 == 0;
}

namespace detail {

template <typename Resource, typename Resources, typename SourceValue, typename Matches, typename Compatible>
inline const Resource * find_full_ordered_value_resource(const Graph & graph,
ValueId graph_value,
const Resources & resources,
SourceValue source_value,
Matches matches,
Compatible compatible) {
auto find_exact = [&](ValueId candidate_id) -> const Resource * {
for (const Resource & resource : resources) {
if (source_value(resource) == candidate_id && matches(resource)) {
return &resource;
}
}
return nullptr;
};

const Resource * exact = find_exact(graph_value);
if (exact != nullptr) {
return exact;
}
Expand All @@ -74,42 +102,75 @@ inline const CommandPlanAlternateValue * find_alternate_value(const Graph &
return nullptr;
}

auto find_if_same_range = [&](ValueId candidate_id) -> const CommandPlanAlternateValue * {
auto find_if_compatible = [&](ValueId candidate_id) -> const Resource * {
const Value * candidate = graph.values().find(candidate_id);
if (candidate == nullptr || !same_full_value_range(*value, *candidate)) {
if (candidate == nullptr) {
return nullptr;
}
return find_alternate_value(plan, candidate_id, type, byte_count);
const Resource * resource = find_exact(candidate_id);
return resource != nullptr && compatible(*resource, *value, *candidate) ? resource : nullptr;
};

ValueId alias = value->alias_source;
for (size_t i = 0; alias.value >= 0 && i < graph.values().size(); ++i) {
const CommandPlanAlternateValue * alternate = find_if_same_range(alias);
if (alternate != nullptr) {
return alternate;
}
const Value * alias_value = graph.values().find(alias);
if (alias_value == nullptr) {
break;
}
alias = alias_value->alias_source;
}

const CommandPlanAlternateValue * root_alternate = find_if_same_range(value->storage_root);
if (root_alternate != nullptr) {
return root_alternate;
if (!graph.has_index()) {
return nullptr;
}

for (const CommandPlanAlternateValue & alternate : plan.metadata.alternate_values()) {
if (alternate.type != type || alternate.byte_count != byte_count) {
continue;
const Value * current = value;
for (size_t i = 0; i < graph.values().size(); ++i) {
const GraphNode * alias = graph.index().producer(current->id);
if (alias == nullptr || (alias->op != GGML_OP_VIEW && alias->op != GGML_OP_RESHAPE) ||
alias->output != current->id || alias->inputs.size() != 1 || current->alias_source != alias->inputs[0] ||
!is_layout_alias_node(graph, *alias)) {
break;
}
const Value * alternate_value = graph.values().find(alternate.graph_value);
if (alternate_value != nullptr && same_full_value_range(*value, *alternate_value)) {
return &alternate;
const Value * source = graph.values().find(alias->inputs[0]);
if (source == nullptr || !same_full_ordered_value_range(*current, *source)) {
break;
}
const Resource * resource = find_if_compatible(source->id);
if (resource != nullptr) {
return resource;
}
current = source;
}
return nullptr;
}

} // namespace detail

inline const CommandPlanAlternateValue * find_alternate_value(const Graph & graph,
const CommandPlan & plan,
ValueId graph_value,
ggml_type type,
size_t byte_count) {
return detail::find_full_ordered_value_resource<CommandPlanAlternateValue>(
graph, graph_value, plan.metadata.alternate_values(),
[](const CommandPlanAlternateValue & alternate) { return alternate.graph_value; },
[=](const CommandPlanAlternateValue & alternate) {
return alternate.type == type && alternate.byte_count == byte_count;
},
[](const CommandPlanAlternateValue & alternate, const Value & value, const Value & source) {
return alternate.type == GGML_TYPE_F16 ? same_full_ordered_value_range(value, source) :
alternate.type == GGML_TYPE_Q8_1 ? same_q8_1_x4_geometry(value, source) :
same_packed_row_geometry(value, source);
});
}

inline const CommandPlanGeneratedResource * find_generated_resource(const Graph & graph,
const CommandPlan & plan,
ValueId graph_value,
GeneratedResourceRole role,
size_t byte_count) {
return detail::find_full_ordered_value_resource<CommandPlanGeneratedResource>(
graph, graph_value, plan.metadata.generated_resources(),
[](const CommandPlanGeneratedResource & resource) { return resource.source_value; },
[=](const CommandPlanGeneratedResource & resource) {
return resource.role == role && resource.byte_count == byte_count;
},
[](const CommandPlanGeneratedResource & resource, const Value & value, const Value & source) {
return resource.role == GeneratedResourceRole::F16K16Major ? same_packed_row_geometry(value, source) :
value.ne == source.ne;
});
}

} // namespace ggml::hrx
18 changes: 18 additions & 0 deletions ggml/src/ggml-hrx/dispatch/command-program-dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ static std::string format_kernel_json(const CommandProgram & program,
out << " \"byte_count\": " << program.completion_counters.byte_count << ",\n";
out << " \"count\": " << program.completion_counters.count << "\n";
out << " },\n";
out << " \"activation_publications\": [\n";
for (size_t i = 0; i < program.activation_publication_diagnostics.size(); ++i) {
const CommandPlanActivationPublicationDiagnostic & diagnostic =
program.activation_publication_diagnostics[i];
if (i > 0) {
out << ",\n";
}
out << " {\"source_value\": " << diagnostic.source_value.value
<< ", \"alternate_value\": " << diagnostic.alternate_value.value
<< ", \"consumer_value\": " << diagnostic.consumer_value.value
<< ", \"requested_format\": \"" << json_escape(diagnostic.requested_format)
<< "\", \"publication_name\": \"" << json_escape(diagnostic.publication_name)
<< "\", \"publication_stage\": \"" << json_escape(diagnostic.publication_stage)
<< "\", \"alias_depth\": " << diagnostic.alias_depth
<< ", \"mixed_fanout\": " << (diagnostic.mixed_fanout ? "true" : "false")
<< ", \"fallback_reason\": \"" << json_escape(diagnostic.fallback_reason) << "\"}";
}
out << "\n ],\n";
out << " \"commands\": [\n";
bool wrote_any = false;
if (!program.initialization_commands.empty()) {
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-hrx/dispatch/command-program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ CommandProgram build_command_program(const Graph & graph,
initialization.data,
});
}
result.activation_publication_diagnostics = plan.metadata.activation_publication_diagnostics();
return result;
}

Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-hrx/dispatch/command-program.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct CommandProgram {
TransientPlan transients;
CompletionCounterPlan completion_counters;
std::vector<ConstantInitialization> constant_initializations;
std::vector<CommandPlanActivationPublicationDiagnostic> activation_publication_diagnostics;
Status status;

bool valid() const { return status.success(); }
Expand Down
2 changes: 1 addition & 1 deletion ggml/src/ggml-hrx/dispatch/dispatch-scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static bool try_match_registration(const Graph & graph,
DispatchMatch & match,
DispatchMatchDiagnostics * diagnostics) {
const DispatchMatchContext context = {
graph, node, node_index, covered_nodes, plan, next_plan_value,
graph, node, node_index, covered_nodes, plan, next_plan_value, &registry,
};
return registry.match(context, match, diagnostics);
}
Expand Down
6 changes: 6 additions & 0 deletions ggml/src/ggml-hrx/dispatch/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ inline constexpr const char kQ5KSymmetricI8K256Row64Layout[] = "q5k-symi8-k25
inline constexpr const char kQ6KI8K32Row64Layout[] = "q6k-i8-k32-row64";
inline constexpr const char kQ6KPackedK256Row64ScaleRowLayout[] = "q6k-packed-k256-row64-scalerow";

enum class WorkloadSpecialization : uint8_t {
Exact,
Dynamic,
};

struct KernelSpecialization {
uint64_t kernel_id = kUncatalogedKernelId;
WorkloadSpecialization workload_specialization = WorkloadSpecialization::Exact;
std::map<std::string, int64_t> integer_parameters;
std::map<std::string, std::string> compile_parameters;
};
Expand Down
Loading
Loading