Skip to content

Custom cuda and metal kernels for system validation#274

Open
Luthaf wants to merge 3 commits into
metatensor:metatomic-corefrom
Luthaf:kernels
Open

Custom cuda and metal kernels for system validation#274
Luthaf wants to merge 3 commits into
metatensor:metatomic-corefrom
Luthaf:kernels

Conversation

@Luthaf

@Luthaf Luthaf commented Jun 30, 2026

Copy link
Copy Markdown
Member

Add custom cuda and metal kernels for system validation. The intention is to add more kernels for unit conversion & any other work that needs to be done inside mta_execute_model.

No tests other than making sure the code compile for now, they will be added once we have Python bindings.

Contributor (creator of pull-request) checklist

  • Tests updated (for new features and bugfixes)?
  • Documentation updated (for new features)?
  • Issue referenced (for PRs that solve an issue)?

Reviewer checklist

  • CHANGELOG updated with public API or any other important changes?

@Luthaf Luthaf requested a review from RMeli June 30, 2026 09:19
@Luthaf Luthaf force-pushed the kernels branch 3 times, most recently from c2584f3 to 607c96a Compare July 8, 2026 13:10
Comment thread metatomic-core/src/kernels/cpu.rs
Comment thread metatomic-core/cmake/metatomic-config.in.cmake
Comment thread metatomic-core/Cargo.toml
Comment thread metatomic-core/src/kernels/cpu.rs Outdated
Comment on lines +56 to +65
let cell_array: ArrayView2<f64> = cell.try_into()?;
for i in 0..3 {
if !pbc_array[i] && !cell_array.row(i).iter().all(|&x| x == 0.0) {
return Err(Error::InvalidParameter(format!(
"invalid cell: for non-periodic dimensions, the corresponding \
cell vector must be zero, but cell[{}] contains non-zero values",
i
)));
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is essentially the same as above, but with a different type for ArrayView2. An helper function would avoid code duplication (and make it easier to add support to more exotic types down the line if needed).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed with a macro (rust generics don't support unconstrained "template parameters", they always require "concepts" i.e. traits for all parameters)

Comment thread metatomic-core/src/kernels/cuda.rs Outdated
int i = threadIdx.x;
if (i < 3) {
if (!pbc[i]) {
if (cell[3 * i] != T(0) || cell[3 * i + 1] != T(0) || cell[3 * i + 2] != T(0)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given this indexing, worth checking that DLPack tensors are contiguous in memory before calling the kernel? If I understand correctly, they could potentially be strided in general.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

very good point!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was quite painful and added a bunch of code, but should be fine now

Comment on lines +44 to +48
eprintln!(
"is_equal_i32 for device {:?} is not implemented",
tensor.device()
);
Ok(())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't this be an error?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know since these are only checks that don't change the model behavior, I would rather skip the checks than error here, to make it easier to add new devices in metatomic without having to add code everywhere

Comment on lines +87 to +91
eprintln!(
"Cell/PBC validation for device {:?} is not implemented",
pbc.device()
);
Ok(())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As above, shouldn't this be an error?

@Luthaf Luthaf force-pushed the kernels branch 3 times, most recently from 7d61507 to d25d254 Compare July 10, 2026 13:23
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