Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Implement K Means #3159

Merged
merged 6 commits into from
Dec 5, 2024
Merged

[ENH] Implement K Means #3159

merged 6 commits into from
Dec 5, 2024

Conversation

sanketkedia
Copy link
Contributor

@sanketkedia sanketkedia commented Nov 19, 2024

Description of changes

Summarize the changes made by this PR.

  • New functionality
    • Implement the full k-means with regularization and random sampling fully emulating the spann reference implementation

Test plan

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Documentation Changes

None

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link

Please tag your PR title with one of: [ENH | BUG | DOC | TST | BLD | PERF | TYP | CLN | CHORE]. See https://docs.trychroma.com/contributing#contributing-code-and-ideas

@sanketkedia sanketkedia changed the title implement k means [ENH] Implement K Means Nov 19, 2024
@sanketkedia sanketkedia requested a review from HammadB November 19, 2024 01:41
@sanketkedia sanketkedia marked this pull request as ready for review November 19, 2024 01:41

struct KMeansAlgorithmInput<'referred_data> {
indices: Vec<u32>,
embeddings: &'referred_data [f32],
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit - might be nice to have a wrapper over flat embedding / embedding dimension

Copy link
Contributor Author

@sanketkedia sanketkedia Dec 5, 2024

Choose a reason for hiding this comment

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

need to look at this wholistically since there are a few other places in other PRs also that have flat embeddings. Cut out a task in tracker for this.

let mut centers = vec![vec![0.0; self.input.embedding_dimension]; self.input.k];
for center in centers.iter_mut() {
let random_center = rand::thread_rng().gen_range(self.input.first..batch_end);
center.copy_from_slice(
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we have to copy? can we use index like we do for input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, we can for center init but then it needs some custom code that can't be reused as it is done currently. In the interest of time, I'll add a TODO and revisit later if performance becomes a concern

previous_counts: &[usize],
) -> (i32, f32) {
let point_idx = self.input.indices[idx];
let dim = self.input.embedding_dimension;
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we had helper structs for the embedding / dimension this would be made cleaner

@sanketkedia sanketkedia force-pushed the 11-06-implement_apply_materialized branch from 9744dbf to d04db2f Compare December 3, 2024 18:35
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch 4 times, most recently from 4bcb63e to 13b81e0 Compare December 4, 2024 17:20
@sanketkedia sanketkedia force-pushed the 11-06-implement_apply_materialized branch from f722f02 to 71771cf Compare December 4, 2024 17:39
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from 13b81e0 to d3bf83a Compare December 4, 2024 17:39
@sanketkedia sanketkedia force-pushed the 11-06-implement_apply_materialized branch from 71771cf to 97dcbde Compare December 4, 2024 18:10
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from d3bf83a to 1497e1b Compare December 4, 2024 18:10
@sanketkedia sanketkedia force-pushed the 11-06-implement_apply_materialized branch 2 times, most recently from dc505fe to fc6a644 Compare December 4, 2024 19:14
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from 1497e1b to 92154e3 Compare December 4, 2024 19:15
@sanketkedia sanketkedia changed the base branch from 11-06-implement_apply_materialized to graphite-base/3159 December 4, 2024 19:24
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from 92154e3 to e131daa Compare December 4, 2024 19:25
@sanketkedia sanketkedia changed the base branch from graphite-base/3159 to main December 4, 2024 19:25
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from e131daa to 4a761d6 Compare December 4, 2024 19:25
@sanketkedia sanketkedia force-pushed the 11-13-implement_k_means branch from 4a761d6 to d72dd6f Compare December 5, 2024 08:54
Copy link
Contributor Author

sanketkedia commented Dec 5, 2024

Merge activity

  • Dec 5, 4:42 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Dec 5, 4:42 AM EST: A user merged this pull request with Graphite.

@sanketkedia sanketkedia merged commit 835780b into main Dec 5, 2024
72 checks passed
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