Skip to content

Conversation

@kahmed10
Copy link
Collaborator

@kahmed10 kahmed10 commented Feb 9, 2026

Motivation

Technical Details

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

{
if(finder.is_candidate(ins))
groups[finder.group_key(ins)].push_back(ins);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use group_by instead otherwise this wont find groups of independent instructions:

std::vector<instruction_ref> groups;
std::copy_if(...);
std::unordered_map<instruction_ref, std::size_t> pos;
std::adjacent_difference(...);
auto pred = [&](instruction_ref x, instruciton_ref y) {
    if(a == b)
        return true;
    if(x->get_operator() != y->get_operator())
        return false;
    if(finder.group_key(a) == finder.group_key(b))
        return false;
    if(pos.at(a) < pos.at(b))
        return reaches(a, b);
    return reaches(b, a);
};
auto each = [&](auto start, auto last) {
    auto n = std::distance(start, last);
    if(n < finder.min_group_size())
        return;
    ...
};
group_by(groups.begin(), groups.end(), each, pred);

return false;
}
return true;
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked in the group_key not here.

auto emb = g->inputs().at(0);
emb_inputs.push_back(emb);
emb_offsets.push_back(cumulative_offset);
cumulative_offset += emb->get_shape().lens().front();
Copy link
Collaborator

@pfultz2 pfultz2 Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should be a transform_partial_sum.

for(auto g : gathers)
{
auto emb = g->inputs().at(0);
emb_inputs.push_back(emb);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this vector is needed thought, It can be easily computed directly.

return {};

// Insert after the last gather in the group (already sorted by position)
auto insert_pt = std::next(gathers.back());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The insert point should be passed into the fuse function.

continue;

assert(replacements.size() == group.size());
for(std::size_t i = 0; i < group.size(); i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use our for_each algorithm that takes two sequence(like std::transform).

std::vector<instruction_ref> adjusted_idx_inputs;
std::vector<std::size_t> idx_sizes;

for(std::size_t i = 0; i < gathers.size(); i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for_each that takes two sequences.

{"ends", std::vector<int64_t>{end}}}),
batched_gather));

slice_offset += idx_size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compute start using partial_sum.

{
m.replace_instruction(group[i], replacements[i]);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the replacements we need to call move_output_instructions_after so the outputs come after inserted operator. This is in fuse_pointwise. It needs to be moved to the module class and unit tests added. I can try and an open a PR for that. It also needs to be fixed because it doesnt work with submodules. For now you will need to add a check like we doin fuse_pointwise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it out here in #4601.


fuse_horizontal_ops(m, gather_horizontal_fusion{});

dead_code_elimination{}.apply(m);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, just run this pass afterwards in the target passes.

run_pass(m1);
EXPECT(m1 == m2);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more tests with more complicated graphs as well such as using the first gather before the second gather.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants