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

[Metal] Pass random seed to metal backend #3724

Merged
merged 4 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 3 additions & 7 deletions taichi/backends/metal/kernel_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class KernelManager::Impl {
print_mem_->size());
TI_ASSERT(print_buffer_ != nullptr);

init_runtime_buffer(compiled_runtime_module_);
init_runtime_buffer(compiled_runtime_module_, params.config->random_seed);
clear_print_assert_buffer();
}

Expand Down Expand Up @@ -756,14 +756,10 @@ class KernelManager::Impl {
}

private:
void init_runtime_buffer(const CompiledRuntimeModule &rtm_module) {
void init_runtime_buffer(const CompiledRuntimeModule &rtm_module, int random_seed) {
char *addr = runtime_mem_->ptr();
// init rand_seeds
// TODO(k-ye): Provide a way to use a fixed seed in dev mode.
std::mt19937 generator(
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
std::default_random_engine generator((unsigned int)random_seed);
std::uniform_int_distribution<uint32_t> distr(
0, std::numeric_limits<uint32_t>::max());
for (int i = 0; i < kNumRandSeeds; ++i) {
Expand Down
20 changes: 8 additions & 12 deletions tests/python/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
from taichi import approx


def archs_support_random(func):
ailzhang marked this conversation as resolved.
Show resolved Hide resolved
return ti.archs_excluding(ti.metal)(func)


@ti.test(exclude=ti.metal)
@ti.test()
def test_random_float():
for precision in [ti.f32, ti.f64]:
ti.init()
Expand All @@ -25,7 +21,7 @@ def fill():
assert (X**i).mean() == approx(1 / (i + 1), rel=1e-2)


@ti.test(exclude=ti.metal)
@ti.test()
def test_random_int():
for precision in [ti.i32, ti.i64]:
ti.init()
Expand All @@ -48,7 +44,7 @@ def fill():
assert (X**i).mean() == approx(1 / (i + 1), rel=1e-2)


@ti.test(exclude=ti.metal)
@ti.test()
def test_random_independent_product():
n = 1024
x = ti.field(ti.f32, shape=n * n)
Expand All @@ -66,7 +62,7 @@ def fill():
assert X.mean() == approx(1 / 4, rel=1e-2)


@ti.test(exclude=ti.metal)
@ti.test()
def test_random_2d_dist():
n = 8192

Expand All @@ -89,7 +85,7 @@ def gen():
assert counters[c] / n == approx(1 / 4, rel=0.2)


@ti.test(exclude=ti.metal)
@ti.test()
def test_random_seed_per_launch():
n = 10
x = ti.field(ti.f32, shape=n)
Expand All @@ -107,7 +103,7 @@ def gen(i: ti.i32):
assert count <= n * 0.15


@ti.test(arch=[ti.cpu, ti.cuda])
@ti.test(arch=[ti.cpu, ti.cuda, ti.metal])
def test_random_seed_per_program():
import numpy as np
n = 10
Expand All @@ -128,7 +124,7 @@ def gen():
assert not np.allclose(result[0], result[1])


@ti.test(arch=[ti.cpu, ti.cuda])
@ti.test(arch=[ti.cpu, ti.cuda, ti.metal])
k-ye marked this conversation as resolved.
Show resolved Hide resolved
def test_random_f64():
'''
Tests the granularity of float64 random numbers.
Expand All @@ -148,7 +144,7 @@ def foo():
assert np.max(frac) > 0


@ti.test(exclude=ti.metal)
@ti.test()
def test_randn():
'''
Tests the generation of Gaussian random numbers.
Expand Down