mlx/tests/load_tests.cpp

148 lines
4.5 KiB
C++
Raw Normal View History

2023-12-01 03:12:53 +08:00
// Copyright © 2023 Apple Inc.
2023-11-30 02:52:08 +08:00
#include <filesystem>
#include <stdexcept>
#include <vector>
#include "doctest/doctest.h"
#include "mlx/mlx.h"
using namespace mlx::core;
std::string get_temp_file(const std::string& name) {
return std::filesystem::temp_directory_path().append(name);
}
2023-12-18 02:19:08 +08:00
TEST_CASE("test tokenizer") {
auto raw = std::string(" { \"testing\": [1 , \"test\"]} ");
auto tokenizer = io::Tokenizer(raw.c_str(), raw.size());
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::CURLY_OPEN);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::STRING);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::COLON);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::ARRAY_OPEN);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::NUMBER);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::COMMA);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::STRING);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::ARRAY_CLOSE);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::CURLY_CLOSE);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::NULL_TYPE);
2023-12-18 10:13:10 +08:00
raw = std::string(" { \"testing\": \"test\"} ");
tokenizer = io::Tokenizer(raw.c_str(), raw.size());
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::CURLY_OPEN);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::STRING);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::COLON);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::STRING);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::CURLY_CLOSE);
CHECK_EQ(tokenizer.getToken().type, io::TOKEN::NULL_TYPE);
2023-12-18 02:19:08 +08:00
}
2023-12-18 22:53:50 +08:00
TEST_CASE("test parseJson") {
2023-12-18 10:13:10 +08:00
auto raw = std::string("{}");
auto res = io::parseJson(raw.c_str(), raw.size());
CHECK(res.is_type(io::JSONNode::Type::OBJECT));
2023-12-18 22:53:50 +08:00
2023-12-18 10:13:10 +08:00
raw = std::string("[]");
res = io::parseJson(raw.c_str(), raw.size());
CHECK(res.is_type(io::JSONNode::Type::LIST));
2023-12-18 22:53:50 +08:00
2023-12-18 23:00:10 +08:00
raw = std::string("[");
CHECK_THROWS_AS(io::parseJson(raw.c_str(), raw.size()), std::runtime_error);
2023-12-18 10:13:10 +08:00
raw = std::string("[{}, \"test\"]");
res = io::parseJson(raw.c_str(), raw.size());
CHECK(res.is_type(io::JSONNode::Type::LIST));
CHECK_EQ(res.getList()->size(), 2);
CHECK(res.getList()->at(0)->is_type(io::JSONNode::Type::OBJECT));
CHECK(res.getList()->at(1)->is_type(io::JSONNode::Type::STRING));
MESSAGE(res.getList()->at(1)->getString());
CHECK_EQ(res.getList()->at(1)->getString(), "test");
2023-12-18 22:53:50 +08:00
2023-12-18 10:13:10 +08:00
raw = std::string("{\"test\": \"test\", \"test_num\": 1}");
res = io::parseJson(raw.c_str(), raw.size());
CHECK(res.is_type(io::JSONNode::Type::OBJECT));
CHECK_EQ(res.getObject()->size(), 2);
CHECK(res.getObject()->at("test")->is_type(io::JSONNode::Type::STRING));
CHECK_EQ(res.getObject()->at("test")->getString(), "test");
CHECK(res.getObject()->at("test_num")->is_type(io::JSONNode::Type::NUMBER));
2023-12-18 22:53:50 +08:00
CHECK_EQ(res.getObject()->at("test_num")->getNumber(), 1);
2023-12-18 10:13:10 +08:00
raw = std::string("{\"test\": { \"test\": \"test\"}}");
res = io::parseJson(raw.c_str(), raw.size());
CHECK(res.is_type(io::JSONNode::Type::OBJECT));
CHECK_EQ(res.getObject()->size(), 1);
CHECK(res.getObject()->at("test")->is_type(io::JSONNode::Type::OBJECT));
CHECK_EQ(res.getObject()->at("test")->getObject()->size(), 1);
CHECK(res.getObject()->at("test")->getObject()->at("test")->is_type(
io::JSONNode::Type::STRING));
}
2023-12-18 02:19:08 +08:00
2023-11-30 02:52:08 +08:00
TEST_CASE("test single array serialization") {
// Basic test
{
auto a = random::uniform(-5.f, 5.f, {2, 5, 12}, float32);
std::string file_path = get_temp_file("test_arr.npy");
save(file_path, a);
auto b = load(file_path);
CHECK_EQ(a.dtype(), b.dtype());
CHECK_EQ(a.shape(), b.shape());
CHECK(array_equal(a, b).item<bool>());
}
// Other shapes
{
auto a = random::uniform(
-5.f,
5.f,
{
1,
},
float32);
std::string file_path = get_temp_file("test_arr_0.npy");
save(file_path, a);
auto b = load(file_path);
CHECK_EQ(a.dtype(), b.dtype());
CHECK_EQ(a.shape(), b.shape());
CHECK(array_equal(a, b).item<bool>());
}
{
auto a = random::uniform(
-5.f,
5.f,
{
46,
},
float32);
std::string file_path = get_temp_file("test_arr_1.npy");
save(file_path, a);
auto b = load(file_path);
CHECK_EQ(a.dtype(), b.dtype());
CHECK_EQ(a.shape(), b.shape());
CHECK(array_equal(a, b).item<bool>());
}
{
auto a = random::uniform(-5.f, 5.f, {5, 2, 1, 3, 4}, float32);
std::string file_path = get_temp_file("test_arr_2.npy");
save(file_path, a);
auto b = load(file_path);
CHECK_EQ(a.dtype(), b.dtype());
CHECK_EQ(a.shape(), b.shape());
CHECK(array_equal(a, b).item<bool>());
}
}