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: 3 additions & 3 deletions .github/workflows/build-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ jobs:

- name: setup libmetatensor
run: |
curl --location -O https://github.com/metatensor/metatensor/releases/download/metatensor-core-v0.2.3/metatensor-core-cxx-0.2.3.tar.gz
tar xf metatensor-core-cxx-0.2.3.tar.gz
cmake -B build-metatensor -S metatensor-core-cxx-0.2.3 \
curl --location -O https://github.com/metatensor/metatensor/releases/download/metatensor-core-v0.2.4/metatensor-core-cxx-0.2.4.tar.gz
tar xf metatensor-core-cxx-0.2.4.tar.gz
cmake -B build-metatensor -S metatensor-core-cxx-0.2.4 \
-DMETATENSOR_INSTALL_BOTH_STATIC_SHARED=OFF \
-DCMAKE_INSTALL_PREFIX=$CMAKE_PREFIX_PATH \
-DCMAKE_BUILD_TYPE=Debug
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/rust-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jobs:
- name: install valgrind
if: matrix.do-valgrind
run: |
sudo apt-get update
sudo apt-get install -y valgrind

- name: Setup sccache
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/torch-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
- name: install valgrind
if: matrix.do-valgrind
run: |
sudo apt-get update
sudo apt-get install -y valgrind

- name: Setup sccache
Expand Down
2 changes: 1 addition & 1 deletion metatomic-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function(check_compatible_versions _actual_ _requested_)
endfunction()


set(REQUIRED_METATENSOR_VERSION "0.2.0")
set(REQUIRED_METATENSOR_VERSION "0.2.4")
# Either metatensor is built as part of the same CMake project, or we try to
# find the corresponding CMake package
if (TARGET metatensor)
Expand Down
2 changes: 1 addition & 1 deletion metatomic-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ name = "metatomic"
bench = false

[dependencies]
metatensor = { version = "0.5.0" }
metatensor = { version = "0.5.1" }
dlpk = { version = "0.4", features = ["ndarray"]}
json = "0.12"
libloading = "0.9"
Expand Down
10 changes: 2 additions & 8 deletions metatomic-core/tests/cxx/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,14 @@ static metatomic::DLPackTensor cell_tensor() {
}

static metatomic::DLPackTensor pbc_tensor() {
// `SimpleDataArray<bool>` does not compile (`std::vector<bool>` has no
// `data()` method), so we use `uint8_t` and patch the dtype code to
// `kDLBool`.
auto array = std::make_unique<metatensor::SimpleDataArray<uint8_t>>(
auto array = std::make_unique<metatensor::SimpleDataArray<bool>>(
std::vector<uintptr_t>{3}, std::vector<uint8_t>{1, 0, 1}
);
auto mts = metatensor::DataArrayBase::to_mts_array(std::move(array));

DLDevice cpu = {kDLCPU, 0};
DLPackVersion version = {DLPACK_MAJOR_VERSION, DLPACK_MINOR_VERSION};
auto* tensor = mts.as_dlpack(cpu, nullptr, version);
tensor->dl_tensor.dtype.code = DLDataTypeCode::kDLBool;

return metatomic::DLPackTensor(tensor);
return metatomic::DLPackTensor(mts.as_dlpack(cpu, nullptr, version));
}

static metatomic::System test_system(size_t n_atoms = 4) {
Expand Down
92 changes: 6 additions & 86 deletions metatomic-core/tests/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,18 @@ template <typename T> static DLManagedTensorVersioned* pbc_tensor() {
return mts.as_dlpack(cpu, nullptr, version);
}


/// SimpleDataArray<bool> doesn't compile (std::vector<bool> has no data()
/// method). We use SimpleDataArray<uint8_t> and patch the dtype code
/// from kDLUInt to kDLBool.
/// `SimpleDataArray<bool>` stores data as `uint8_t` internally, so the data
/// vector must use `uint8_t` as well.
template <> DLManagedTensorVersioned* pbc_tensor<bool>() {
std::vector<uint8_t> pbc_data = {1, 0, 1};
auto array = std::make_unique<metatensor::SimpleDataArray<uint8_t>>(
auto array = std::make_unique<metatensor::SimpleDataArray<bool>>(
std::vector<uintptr_t>{3},
std::move(pbc_data)
);
auto mts = metatensor::DataArrayBase::to_mts_array(std::move(array));
DLDevice cpu = {kDLCPU, 0};
DLPackVersion version = {DLPACK_MAJOR_VERSION, DLPACK_MINOR_VERSION};
auto* tensor = mts.as_dlpack(cpu, nullptr, version);

tensor->dl_tensor.dtype.code = DLDataTypeCode::kDLBool;

return tensor;
return mts.as_dlpack(cpu, nullptr, version);
}

static mts_block_t* pair_block() {
Expand Down Expand Up @@ -716,87 +710,13 @@ static void check_full_system_data(const mta_system_t* system) {
CHECK(retrieved != nullptr);
}

/// `DataArrayBase` storing boolean data as `uint8_t` (since
/// `std::vector<bool>` has no `data()` method, `SimpleDataArray<bool>` can not
/// be used). This class reports its dtype as `kDLBool` so that the metatensor
/// serialization code correctly handles it.
class BoolDataArray: public metatensor::SimpleDataArray<uint8_t> {
public:
using SimpleDataArray::SimpleDataArray;

DLDataType dtype() const override {
DLDataType dtype;
dtype.code = DLDataTypeCode::kDLBool;
dtype.bits = 8;
dtype.lanes = 1;
return dtype;
}

DLManagedTensorVersioned* as_dlpack(
DLDevice device,
const int64_t* stream,
DLPackVersion max_version
) override {
auto* managed = SimpleDataArray::as_dlpack(device, stream, max_version);
managed->dl_tensor.dtype.code = DLDataTypeCode::kDLBool;
return managed;
}

std::unique_ptr<DataArrayBase> copy(DLDevice device) const override {
if (device.device_type != kDLCPU) {
throw metatensor::Error("BoolDataArray only supports copying to CPU");
}
return std::unique_ptr<DataArrayBase>(new BoolDataArray(*this));
}

std::unique_ptr<DataArrayBase> create(
std::vector<uintptr_t> shape,
metatensor::MtsArray fill_value
) const override {
DLDevice cpu_device = {kDLCPU, 0};
DLPackVersion version = {DLPACK_MAJOR_VERSION, DLPACK_MINOR_VERSION};
auto fill_dlpack = fill_value.as_dlpack_array<uint8_t>(cpu_device, nullptr, version);

if (!fill_dlpack.shape().empty()) {
throw metatensor::Error("`fill_value` must be a single scalar");
}

auto scalar = fill_dlpack.data()[0];
return std::unique_ptr<DataArrayBase>(new BoolDataArray(std::move(shape), scalar));
}
};

/// `mts_realloc_buffer_t` callback backed by a `std::vector<uint8_t>`.
static uint8_t* vector_realloc(void* user_data, uint8_t* /*ptr*/, uintptr_t new_size) {
auto* buffer = static_cast<std::vector<uint8_t>*>(user_data);
buffer->resize(new_size, 0);
return buffer->data();
}

/// `mts_create_array_callback_t` that delegates to
/// `metatensor::details::default_create_array`, but handles `kDLBool` by
/// creating a `BoolDataArray` (`SimpleDataArray<bool>` does not compile since
/// `std::vector<bool>` has no `data()` method). Can be removed once
/// https://github.com/metatensor/metatensor/pull/1164 is released.
static mts_status_t create_array_with_bool(
const uintptr_t* shape_ptr,
uintptr_t shape_count,
DLDataType dtype,
mts_array_t* array
) {
if (dtype.code == kDLBool && dtype.bits == 8 && dtype.lanes == 1) {
auto shape = std::vector<uintptr_t>();
for (uintptr_t i = 0; i < shape_count; i++) {
shape.push_back(shape_ptr[i]);
}
auto cxx_array = std::make_unique<BoolDataArray>(shape);
*array = metatensor::DataArrayBase::to_mts_array(std::move(cxx_array)).release();
return MTS_SUCCESS;
}

return metatensor::details::default_create_array(shape_ptr, shape_count, dtype, array);
}

TEST_CASE("system serialization") {
SECTION("save and load to a file") {
auto* system = full_test_system();
Expand All @@ -808,7 +728,7 @@ TEST_CASE("system serialization") {
mta_system_t* loaded = nullptr;
auto status = mta_load(
path.c_str(),
create_array_with_bool,
metatensor::details::default_create_array,
&loaded
);
CHECK(status == MTA_SUCCESS);
Expand Down Expand Up @@ -837,7 +757,7 @@ TEST_CASE("system serialization") {
mta_system_t* loaded = nullptr;
status = mta_load_buffer(
buffer.data(), buffer.size(),
create_array_with_bool,
metatensor::details::default_create_array,
&loaded
);
CHECK(status == MTA_SUCCESS);
Expand Down
2 changes: 1 addition & 1 deletion metatomic-core/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub fn setup_torch_pip(python: &Path) -> PathBuf {
/// Install metatensor in a Python virtualenv with pip, and return the
/// CMAKE_PREFIX_PATH for the installed libmetatensor.
pub fn setup_metatensor_pip(python: &Path) -> PathBuf {
pip_install(python, &["metatensor-core >=0.2.2,<0.3"], PipInstallOptions::default());
pip_install(python, &["metatensor-core >=0.2.4,<0.3"], PipInstallOptions::default());

let mut cmd = Command::new(python);
cmd.arg("-c");
Expand Down
2 changes: 1 addition & 1 deletion python/metatomic_core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ requires = [
"setuptools >=77",
"packaging >=26",
"cmake",
"metatensor-core >=0.2.2,<0.3",
"metatensor-core >=0.2.4,<0.3",
]

build-backend = "setuptools.build_meta"
Expand Down
Loading