#include "doctest/doctest.h"
#include "mlx/mlx.h"
#include "mlx/scheduler.h"
using namespace mlx::core;
TEST_CASE("test stream management") {
auto s1 = default_stream(default_device());
CHECK_EQ(s1.device, default_device());
auto s2 = new_stream(default_device());
CHECK_EQ(s2.device, default_device());
CHECK_NE(s1, s2);
if (gpu::is_available()) {
auto s_gpu = default_stream(Device::gpu);
CHECK_EQ(s_gpu.device, Device::gpu);
} else {
CHECK_THROWS_AS(default_stream(Device::gpu), std::invalid_argument);
}
auto s_cpu = default_stream(Device::cpu);
CHECK_EQ(s_cpu.device, Device::cpu);
s_cpu = new_stream(Device::cpu);
CHECK_EQ(s_cpu.device, Device::cpu);
if (gpu::is_available()) {
auto s_gpu = new_stream(Device::gpu);
CHECK_EQ(s_gpu.device, Device::gpu);
} else {
CHECK_THROWS_AS(new_stream(Device::gpu), std::invalid_argument);
}
}
TEST_CASE("test default stream in threads") {
std::set<Stream> thread_streams;
std::mutex mtx;
std::vector<std::thread> threads;
auto all_streams = get_streams();
size_t num_streams = all_streams.size();
size_t num_threads = 4;
for (size_t i = 0; i < num_threads; ++i) {
threads.emplace_back([&thread_streams, &mtx]() {
auto s = default_stream(gpu::is_available() ? Device::gpu : Device::cpu);
{
std::lock_guard lock(mtx);
thread_streams.insert(s);
}
clear_streams();
});
}
for (auto& t : threads) {
t.join();
}
CHECK_EQ(thread_streams.size(), num_threads);
all_streams = get_streams();
CHECK_EQ(all_streams.size() - num_streams, num_threads);
std::set new_streams(all_streams.begin() + num_streams, all_streams.end());
CHECK_EQ(new_streams, thread_streams);
}
TEST_CASE("test access stream in other thread") {
if (!gpu::is_available()) {
return;
}
auto main_thread_stream = new_stream(Device::gpu);
eval(arange(10, main_thread_stream));
bool error_caught = false;
std::thread t([&] {
try {
eval(arange(10, main_thread_stream));
} catch (const std::runtime_error&) {
error_caught = true;
}
clear_streams();
});
t.join();
CHECK(error_caught);
}
TEST_CASE("test new stream in threads") {
std::vector<std::thread> threads;
for (int i = 0; i < 1; ++i) {
threads.emplace_back([]() {
auto s = new_stream(default_device());
eval(arange(10, s));
clear_streams();
});
}
for (auto& t : threads) {
t.join();
}
}
TEST_CASE("test thread local stream") {
auto s = new_thread_local_stream(default_device());
int result = sum(arange(10, s)).item<int>();
std::atomic<int> finished = 0;
std::vector<std::thread> threads;
int num_threads = 4;
for (int i = 0; i < 4; ++i) {
threads.emplace_back([&]() {
int r = sum(arange(10, s)).item<int>();
CHECK_EQ(result, r);
finished += 1;
clear_streams();
});
}
for (auto& t : threads) {
t.join();
}
CHECK_EQ(finished, num_threads);
}
TEST_CASE("test get streams") {
default_stream(Device::cpu);
auto streams = get_streams();
CHECK(streams.size() >= 1);
auto s_cpu = default_stream(Device::cpu);
bool found_cpu = false;
for (auto& s : streams) {
if (s == s_cpu) {
found_cpu = true;
}
}
CHECK(found_cpu);
auto s_new = new_stream(Device::cpu);
streams = get_streams();
bool found_new = false;
for (auto& s : streams) {
if (s == s_new) {
found_new = true;
}
}
CHECK(found_new);
}
TEST_CASE("test asynchronous launch") {
auto s1 = default_stream(Device::cpu);
auto s2 = new_stream(Device::cpu);
int x = 1;
auto p1 = std::make_shared<std::promise<void>>();
auto p2 = std::make_shared<std::promise<void>>();
auto f1 = p1->get_future().share();
auto f2 = p2->get_future().share();
auto fn1 = [&x, p = std::move(p1)]() {
x++;
p->set_value();
};
auto fn2 = [&x, p = std::move(p2), f = std::move(f1)]() {
f.wait();
x *= 5;
p->set_value();
};
scheduler::enqueue(s2, std::move(fn2));
scheduler::enqueue(s1, std::move(fn1));
f2.wait();
CHECK_EQ(x, 10);
}
TEST_CASE("test stream placement") {
auto s1 = default_stream(Device::cpu);
auto s2 = new_stream(Device::cpu);
{
auto p = std::make_shared<std::promise<void>>();
auto f = p->get_future().share();
scheduler::enqueue(s1, [f = std::move(f)]() { f.wait(); });
auto x = zeros({100}, float32, s2);
auto y = ones({100}, float32, s2);
auto z = add(x, y, s2);
eval(z);
p->set_value();
}
{
auto p = std::make_shared<std::promise<void>>();
auto f = p->get_future().share();
scheduler::enqueue(s1, [f = std::move(f)]() { f.wait(); });
auto fn = [&s2](array a) { return add(a, add(a, a, s2), s2); };
auto x = zeros({100}, s2);
auto [out, dout] = vjp(fn, x, ones({100}, s2));
std::tie(out, dout) = jvp(fn, x, ones({100}, s2));
eval(out, dout);
p->set_value();
}
}
TEST_CASE("test scheduler races") {
auto x = zeros({1});
auto y = zeros({100});
eval(x, y);
auto a = exp(x);
eval(a);
a = exp(x);
for (int i = 0; i < 10000; ++i) {
y = exp(y);
}
eval(a, y);
}