File size: 20,746 Bytes
712dbf0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
// Copyright © 2023 Apple Inc.
#include <numeric>
#include "doctest/doctest.h"
#include "mlx/mlx.h"
using namespace mlx::core;
TEST_CASE("test random key") {
auto key = random::key(0);
CHECK(array_equal(key, array({0, 0})).item<bool>());
key = random::key(1);
CHECK(array_equal(key, array({0, 1})).item<bool>());
int64_t seed = static_cast<int64_t>(1) << 32;
key = random::key(seed);
CHECK(array_equal(key, array({1, 0})).item<bool>());
key = random::key(seed + 1);
CHECK(array_equal(key, array({1, 1})).item<bool>());
}
TEST_CASE("test global rng") {
random::seed(4);
auto x = random::bits({});
auto y = random::bits({});
random::seed(4);
auto a = random::bits({});
auto b = random::bits({});
CHECK_EQ(x.item<uint32_t>(), a.item<uint32_t>());
CHECK_EQ(y.item<uint32_t>(), b.item<uint32_t>());
}
TEST_CASE("test random split") {
auto [key, subkey] = random::split(random::key(0));
CHECK(array_equal(key, array({4146024105u, 967050713u})).item<bool>());
CHECK(array_equal(subkey, array({2718843009u, 1272950319u})).item<bool>());
auto keys = random::split(random::key(0), 3);
auto expected = array(
{2467461003u,
428148500u,
3186719485u,
3840466878u,
2562233961u,
1946702221u},
{3, 2});
CHECK(array_equal(keys, expected).item<bool>());
}
TEST_CASE("test random bits") {
// Test shapes, types, and sizes
{
auto key = random::key(0);
auto x = random::bits({}, key);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), uint32);
x = random::bits({0}, key);
CHECK(array_equal(x, array({})).item<bool>());
// Check wrong key type or shape
key = array({0, 0});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0u, 0u, 0u}, {3, 1});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0u, 0u}, {2, 1});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
}
// Expected bits in the following tests were generated from
// Jax's Threefry 2x32 implementation using the following in
// python:
//
// ```
// import jax
// import jax.prng
// shape = (SET THIS)
// seed = (SET THIS)
// width = (SET THIS)
// key = jax.random.PRNGKey(seed)
// print(jax.prng.threefry_prng_impl.random_bits(key, width, shape))
{
auto key = random::key(0);
auto x = random::bits({}, key);
auto y = random::bits({}, key);
CHECK_EQ(x.item<uint32_t>(), 1797259609u);
CHECK_EQ(x.item<uint32_t>(), y.item<uint32_t>());
x = random::bits({}, 2, key);
CHECK_EQ(x.item<uint16_t>(), 345);
x = random::bits({}, 1, key);
CHECK_EQ(x.item<uint8_t>(), 89);
}
{
auto key = random::key(1);
auto x = random::bits({}, key);
CHECK_EQ(x.item<uint32_t>(), 507451445u);
x = random::bits({}, 2, key);
CHECK_EQ(x.item<uint16_t>(), 6197);
x = random::bits({}, 1, key);
CHECK_EQ(x.item<uint8_t>(), 53);
CHECK_THROWS(random::bits({}, 0, key));
CHECK_THROWS(random::bits({}, 5, key));
CHECK_THROWS(random::bits({}, -1, key));
}
{
auto key = random::key(0);
auto x = random::bits({3, 1}, key);
auto expected = array({4146024105u, 1351547692u, 2718843009u}, {3, 1});
CHECK(array_equal(x, expected).item<bool>());
x = random::bits({5}, 2, key);
expected = array({20137, 63263, 64300, 20622, 16513}, uint16);
CHECK(array_equal(x, expected).item<bool>());
expected = array({20137, 63263, 64300, 20622, 16513, 41486}, uint16);
x = random::bits({6}, 2, key);
CHECK(array_equal(x, expected).item<bool>());
expected = array({20137, 63263, 1497, 14756, 16513, 41486, 44591}, uint16);
x = random::bits({7}, 2, key);
CHECK(array_equal(x, expected).item<bool>());
x = random::bits({8}, 2, key);
expected =
array({20137, 63263, 1497, 14756, 16513, 41486, 44591, 19423}, uint16);
CHECK(array_equal(x, expected).item<bool>());
}
{
auto key = array({0u, 0u, 1u, 1u}, {2, 2});
auto shape = Shape{3};
auto fn = [&shape](array k) { return random::bits(shape, k); };
auto expected = array(
{4146024105u,
1351547692u,
2718843009u,
3725146706u,
1802982961u,
1349634643u},
{2, 3});
CHECK(array_equal(vmap(fn)(key), expected).item<bool>());
expected = array(
{2441914641u,
1110694964u,
3819641963u,
2441914641u,
1110694964u,
3819641963u},
{2, 3});
CHECK(array_equal(vmap(fn, 1)(key), expected).item<bool>());
// Vmap twice
key = array(
{0u,
0u,
1u,
1u,
2u,
2u,
3u,
3u,
4u,
4u,
5u,
5u},
{3, 2, 2});
shape = {2};
auto out = vmap(vmap(fn))(key);
expected = array(
{928981903u,
3453687069u,
3606183818u,
460005496u,
2799733733u,
856293553u,
4081856343u,
3445925136u,
2775548010u,
1430281703u,
305173070u,
2615843348u},
{3, 2, 2});
CHECK(array_equal(out, expected).item<bool>());
out = vmap(vmap(fn, 1), 0)(key);
expected = array(
{1948878966u,
4237131848u,
1948878966u,
4237131848u,
2531170506u,
1858648356u,
2531170506u,
1858648356u,
740561898u,
4234094099u,
740561898u,
4234094099u},
{3, 2, 2});
CHECK(array_equal(out, expected).item<bool>());
}
// Vmap smaller type
{
auto key = array({0u, 0u, 1u, 1u}, {2, 2});
auto fn = [](array k) { return random::bits({5}, 2, k); };
auto expected = array(
{4146024105u,
1351547692u,
2718843009u,
3725146706u,
1802982961u,
1349634643u},
{2, 3});
auto out = vmap(fn)(key);
auto x1 = random::bits({5}, 2, take(key, array(0), 0));
auto x2 = random::bits({5}, 2, take(key, array(1), 0));
CHECK(array_equal(take(out, array(0), 0), x1).item<bool>());
CHECK(array_equal(take(out, array(1), 0), x2).item<bool>());
}
}
TEST_CASE("test random uniform") {
// Test shapes, types, and sizes
{
auto x = random::uniform({});
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float32);
x = random::uniform({}, float16);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float16);
x = random::uniform({0});
CHECK(array_equal(x, array({})).item<bool>());
// Non float type throws
CHECK_THROWS_AS(random::uniform({}, int32), std::invalid_argument);
// dtype respected
x = random::uniform(-.1, .1, {0}, bfloat16);
CHECK_EQ(x.dtype(), bfloat16);
// Check broadcasting
x = random::uniform(zeros({3, 1}), ones({1, 3}), {3, 3});
CHECK_EQ(x.shape(), Shape{3, 3});
CHECK_THROWS_AS(
random::uniform(zeros({3, 3}), 1.0, {1, 3}), std::invalid_argument);
CHECK_THROWS_AS(
random::uniform(zeros({3, 3}), 1.0, {2, 3}), std::invalid_argument);
CHECK_THROWS_AS(
random::uniform(zeros({3, 1}), ones({1, 3}), {1, 3}),
std::invalid_argument);
// Check wrong key type or shape
auto key = array({0, 0});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0u, 0u, 0u}, {3, 1});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
key = array({0u, 0u}, {2, 1});
CHECK_THROWS_AS(random::uniform({}, key), std::invalid_argument);
}
// Expected bits in the following tests were generated from
// Jax's Threefry 2x32 implementation using the following in
// python:
//
// ```
// import jax
// import jax.prng
// shape = (SET THIS)
// seed = (SET THIS)
// key = jax.random.PRNGKey(seed)
// print(jax.prng.threefry_prng_impl.random_bits(key, 32, shape))
constexpr auto to_float = [](uint32_t n) {
return static_cast<float>(n) / UINT32_MAX;
};
{
auto key = random::key(0);
auto x = random::uniform({}, key);
auto y = random::uniform({}, key);
auto expected = to_float(1797259609);
CHECK_EQ(x.item<float>(), expected);
CHECK_EQ(x.item<float>(), y.item<float>());
}
{
auto key = random::key(1);
auto x = random::uniform({}, key);
auto expected = to_float(507451445);
CHECK_EQ(x.item<float>(), expected);
}
{
auto key = random::key(0);
auto x = random::uniform({3, 1}, key);
auto expected = array(
{to_float(4146024105), to_float(1351547692), to_float(2718843009)},
{3, 1});
CHECK(array_equal(x, expected).item<bool>());
}
// Check vmap
{
auto key = random::key(0);
auto fun = [](array k, array low) {
return random::uniform(low, 1, {3}, float32, k);
};
auto out = vmap(fun, -1)(key, zeros({2, 3}));
CHECK_EQ(out.shape(), Shape{2, 3});
key = zeros({2, 2}, uint32);
out = vmap(fun)(key, zeros({2, 3}));
CHECK_EQ(out.shape(), Shape{2, 3});
}
// Check bounds are respected
{
auto key = random::key(128291);
auto out = random::uniform(array(-1.0f), array(1.0f), {100}, float32, key);
CHECK(all(less(out, array(1.0f))).item<bool>());
CHECK(all(greater_equal(out, array(-1.0f))).item<bool>());
}
// Check float16
{
auto key = random::key(0);
auto out = random::uniform({1000}, float16, key);
CHECK_EQ(out.dtype(), float16);
CHECK(all(less(out, array(1.0f))).item<bool>());
CHECK(all(greater_equal(out, array(0.0f))).item<bool>());
CHECK(!all(equal(out, array(0.0f))).item<bool>());
CHECK(abs(float(mean(out).item<float16_t>()) - 0.5f) < 0.02);
}
{
auto key = random::key(0);
auto out = random::uniform({1000}, bfloat16, key);
CHECK_EQ(out.dtype(), bfloat16);
CHECK(all(less(out, array(1.0f))).item<bool>());
CHECK(all(greater_equal(out, array(0.0f))).item<bool>());
CHECK(!all(equal(out, array(0.0f))).item<bool>());
CHECK(abs(float(mean(out).item<bfloat16_t>()) - 0.5f) < 0.02);
}
}
TEST_CASE("test random normal") {
// Test shapes, types, and sizes
{
auto x = random::normal({});
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float32);
x = random::uniform({0});
CHECK(array_equal(x, array({})).item<bool>());
// Non float type throws
CHECK_THROWS_AS(random::normal({}, int32), std::invalid_argument);
// Check wrong key type or shape
auto key = array({0, 0});
CHECK_THROWS_AS(random::normal({}, key), std::invalid_argument);
key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(random::normal({}, key), std::invalid_argument);
key = array({0u, 0u, 0u}, {3, 1});
CHECK_THROWS_AS(random::normal({}, key), std::invalid_argument);
key = array({0u, 0u}, {2, 1});
CHECK_THROWS_AS(random::normal({}, key), std::invalid_argument);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::normal({100}, key);
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(mean(out).item<float>()) < 0.1);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::normal({200}, float16, key);
CHECK_EQ(out.dtype(), float16);
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(float(mean(out).item<float16_t>())) < 0.1);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::normal({200}, bfloat16, key);
CHECK_EQ(out.dtype(), bfloat16);
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(float(mean(out).item<bfloat16_t>())) < 0.1);
}
}
TEST_CASE("test random multivariate_normal") {
// Scope switch to the cpu for SVDs
StreamContext sc(Device::cpu);
{
auto mean = zeros({3});
auto cov = eye(3);
auto x = random::multivariate_normal(mean, cov, {1000}, float32);
CHECK_EQ(x.shape(), Shape{1000, 3});
CHECK_EQ(x.dtype(), float32);
}
// Limit case
{
auto mean = array({0, 0});
auto cov = array({1., -1, -.1, 1.});
cov = reshape(cov, {2, 2});
auto x = random::multivariate_normal(mean, cov, {1}, float32);
CHECK_EQ(x.shape(), Shape{1, 2});
CHECK_EQ(x.dtype(), float32);
}
// Check wrong shapes
{
auto mean = zeros({3, 1});
auto cov = eye(3);
CHECK_THROWS_AS(
random::multivariate_normal(
mean,
cov,
{
1000,
},
float32),
std::invalid_argument);
}
{
auto mean = zeros({3});
auto cov = zeros({1, 2, 3, 3});
auto x = random::multivariate_normal(mean, cov, {1000, 2}, float32);
CHECK_EQ(x.shape(), Shape{1000, 2, 3});
}
{
auto mean = zeros({3});
auto cov = eye(4);
CHECK_THROWS_AS(
random::multivariate_normal(mean, cov, {1000, 3}, float32),
std::invalid_argument);
}
// Check wrong type
{
auto mean = zeros({3});
auto cov = eye(3);
CHECK_THROWS_AS(
random::multivariate_normal(mean, cov, {1000, 3}, float16),
std::invalid_argument);
}
}
TEST_CASE("test random randint") {
CHECK_THROWS_AS(
random::randint(array(3), array(5), {1}, float32), std::invalid_argument);
auto x = random::randint(0, 10, {}, uint32);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), uint32);
x = random::randint(0, 2, {}, bool_);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), bool_);
x = random::randint(0, 2, {}, int32);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), int32);
x = random::randint(0, 2, {}, int64);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), int64);
// Check all in bounds
auto low = -10.0;
auto high = 20.0;
x = random::randint(low, high, {1000, 1000});
CHECK((all(low <= x).item<bool>() && all(x < high).item<bool>()));
// Check high < low => all equals to low
low = 20.0;
high = -10.0;
x = random::randint(low, high, {3, 3});
CHECK(all(equal(x, array(low))).item<bool>());
// Check wrong key type or shape
auto key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(
random::randint(low, high, {}, float32, key), std::invalid_argument);
}
TEST_CASE("test random bernoulli") {
auto x = random::bernoulli();
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), bool_);
// Bernoulli parameter can have floating point type
x = random::bernoulli(array(0.5, float16));
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), bool_);
CHECK_THROWS(random::bernoulli(array(1, int32)));
// Negative numbers allowed in Jax
x = random::bernoulli(array(-1.0));
CHECK_FALSE(x.item<bool>());
x = random::bernoulli(array(5.0));
CHECK(x.item<bool>());
// Return array with correct shape
x = random::bernoulli(0.5, {3, 3});
CHECK_EQ(x.shape(), Shape{3, 3});
// Try with p = {}
x = random::bernoulli(array({}));
CHECK_EQ(x.size(), 0);
// Try broadcasting
auto p = array({0.1, 0.2, 0.3});
p = reshape(p, {1, 3});
x = random::bernoulli(p, {4, 3});
CHECK_EQ(x.shape(), Shape{4, 3});
CHECK_THROWS_AS(random::bernoulli(array({}), {3, 3}), std::invalid_argument);
p = array({0.1, 0.2, 0.3});
// Ask for the wrong shape => throws
CHECK_THROWS_AS(random::bernoulli(p, Shape{2}), std::invalid_argument);
// Check wrong key type or shape
auto key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(random::bernoulli(array(0.5), key), std::invalid_argument);
}
TEST_CASE("Test truncated normal") {
auto x = random::truncated_normal(array(-2.0), array(2.0));
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float32);
x = random::truncated_normal(array(-2.0), array(2.0), {}, float16);
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float16);
// Requested shape
x = random::truncated_normal(array(-2.0), array(2.0), {3, 4});
CHECK_EQ(x.shape(), Shape{3, 4});
// Empty array
x = random::truncated_normal(array({}), array({}));
CHECK_EQ(x.size(), 0);
// Broadcast
auto lower = reshape(array({-2.0, -3.0}), {1, 2});
auto higher = reshape(array({0.0, 3.0, 1.5}), {3, 1});
x = random::truncated_normal(lower, higher);
// All in bounds
CHECK_EQ(x.shape(), Shape{3, 2});
CHECK((all(x <= higher).item<bool>() && all(lower <= x).item<bool>()));
// high < low => all equal to low
x = random::truncated_normal(array(2.0), array(-2.0));
CHECK(all(x == array(2.0)).item<bool>());
// Non broadcastable => throws
CHECK_THROWS_AS(
random::truncated_normal(lower, higher, {4, 2}), std::invalid_argument);
auto key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(
random::truncated_normal(array(-2.0), array(2.0), {1, 1}, float32, key),
std::invalid_argument);
}
TEST_CASE("test categorical") {
auto logits = zeros({10, 20});
using random::categorical;
// Invalid axes
CHECK_THROWS(categorical(logits, 2));
CHECK_THROWS(categorical(logits, -3));
// Invalid requested shapes
CHECK_THROWS(categorical(logits, 1, Shape{1}));
CHECK_THROWS(categorical(logits, 1, Shape{11}));
CHECK_THROWS(categorical(logits, 1, {10, 1}));
CHECK_EQ(categorical(logits, -1).shape(), Shape{10});
CHECK_EQ(categorical(logits, 0).shape(), Shape{20});
CHECK_EQ(categorical(logits, 1).shape(), Shape{10});
auto out = categorical(logits);
CHECK_EQ(out.shape(), Shape{10});
CHECK_EQ(out.dtype(), uint32);
CHECK(max(out).item<uint32_t>() < 20);
out = categorical(logits, 0, {5, 20});
CHECK_EQ(out.shape(), Shape{5, 20});
CHECK(max(out).item<uint32_t>() < 10);
float inf = std::numeric_limits<float>::infinity();
logits = array({1.0f, -2.0f, inf, 4.0f, 3.0f});
CHECK_EQ(categorical(logits).item<uint32_t>(), 2);
logits = array({-inf, -2.0f, -inf, -inf});
CHECK_EQ(categorical(logits).item<uint32_t>(), 1);
logits = zeros({5, 4, 3});
CHECK_EQ(categorical(logits, -1, 7).shape(), Shape{5, 4, 7});
CHECK_EQ(categorical(logits, -2, 7).shape(), Shape{5, 3, 7});
CHECK_EQ(categorical(logits, -3, 7).shape(), Shape{4, 3, 7});
}
TEST_CASE("test laplace") {
// Test shapes, types, and sizes
{
auto x = random::laplace({});
CHECK_EQ(x.size(), 1);
CHECK_EQ(x.dtype(), float32);
// Non float type throws
CHECK_THROWS_AS(random::laplace({}, int32), std::invalid_argument);
// Check wrong key type or shape
auto key = array({0, 0});
CHECK_THROWS_AS(random::laplace({}, key), std::invalid_argument);
key = array({0, 0}, {1, 2});
CHECK_THROWS_AS(random::laplace({}, key), std::invalid_argument);
key = array({0u, 0u, 0u}, {3, 1});
CHECK_THROWS_AS(random::laplace({}, key), std::invalid_argument);
key = array({0u, 0u}, {2, 1});
CHECK_THROWS_AS(random::laplace({}, key), std::invalid_argument);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::laplace({1000000}, key);
float sample_mean = mean(out).item<float>();
float sample_variance = var(out).item<float>();
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(sample_mean) < 0.1);
// Chebyshev's inequality.
for (int k = 1; k <= 5; ++k) {
float prob_above =
mean(greater_equal(out, array(k * std::sqrt(sample_variance))))
.item<float>();
float bound = 1 / std::pow(k, 2);
CHECK(prob_above < bound);
}
// Expected variance for Laplace distribution is 2*scale^2.
float expected_variance = 2.0;
CHECK(std::abs(sample_variance - expected_variance) < 0.01);
// Expected kurtosis of Laplace distribution is 3.
array fourth_pows = power(out - sample_mean, array(4));
float sample_kurtosis =
mean(fourth_pows).item<float>() / std::pow(sample_variance, 2) - 3;
float expected_kurtosis = 3.0;
CHECK(std::abs(sample_kurtosis - expected_kurtosis) < 0.1);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::laplace({10000}, float16, key);
CHECK_EQ(out.dtype(), float16);
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(float(mean(out).item<float16_t>())) < 0.1);
}
{
constexpr float inf = std::numeric_limits<float>::infinity();
auto key = random::key(128291);
auto out = random::laplace({10000}, bfloat16, key);
CHECK_EQ(out.dtype(), bfloat16);
CHECK(all(less(abs(out), array(inf))).item<bool>());
CHECK(abs(float(mean(out).item<bfloat16_t>())) < 0.1);
}
}
|