autodiff.in.cpp 45.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------

#include <algorithm>
16
#include <functional>
17
#include <memory>
18
#include <tuple>
19 20 21 22

#include "gtest/gtest.h"

#include "ngraph/ngraph.hpp"
23
#include "util/all_close.hpp"
24 25 26
#include "util/autodiff/backprop_derivative.hpp"
#include "util/autodiff/backprop_function.hpp"
#include "util/autodiff/numeric_derivative.hpp"
27
#include "util/random.hpp"
28 29 30 31

using namespace std;
using namespace ngraph;

32 33 34 35 36 37 38
template <typename T>
bool autodiff_numeric_compare(const std::shared_ptr<runtime::Manager>& manager,
                              const std::shared_ptr<runtime::Backend>& backend,
                              std::function<std::shared_ptr<Function>()> make_graph,
                              const std::vector<std::shared_ptr<runtime::TensorView>>& args,
                              T rtol,
                              T atol)
39
{
40 41 42
    auto f = make_graph();
    auto results_num =
        autodiff::numeric_derivative<T>(manager, backend, f, args, .001f, f->get_parameters());
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
    auto g = make_graph();
    auto results_sym =
        autodiff::backprop_derivative<T>(manager, backend, g, args, g->get_parameters());

    return test::all_close(results_num, results_sym, rtol, atol);
}

template <typename T>
bool autodiff_numeric_compare_selective(
    const std::shared_ptr<runtime::Manager>& manager,
    const std::shared_ptr<runtime::Backend>& backend,
    std::function<std::shared_ptr<Function>()> make_graph,
    const std::vector<std::shared_ptr<runtime::TensorView>>& args,
    T rtol,
    T atol,
    const std::vector<bool>& indep_param_mask)
{
    std::vector<std::shared_ptr<op::Parameter>> f_indep_params;
    auto f = make_graph();

    size_t i = 0;

    for (auto b : indep_param_mask)
    {
        if (b)
        {
            f_indep_params.push_back(f->get_parameters().at(i));
        }
        i++;
    }

    auto results_num =
        autodiff::numeric_derivative<T>(manager, backend, f, args, .001f, f_indep_params);

    std::vector<std::shared_ptr<op::Parameter>> g_indep_params;
    auto g = make_graph();

    i = 0;

    for (auto b : indep_param_mask)
    {
        if (b)
        {
            g_indep_params.push_back(g->get_parameters().at(i));
        }
        i++;
    }

    auto results_sym = autodiff::backprop_derivative<T>(manager, backend, g, args, g_indep_params);

    return test::all_close(results_num, results_sym, rtol, atol);
}

97
TEST(${BACKEND_NAME}, backwards_abs)
98
{
99
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    // The numeric derivative and the symbolic one may disagree around 0, so we will dance around
    // that point by skipping (-0.01,0.01).
    test::Uniform<float> rng_neg(-1.0f, -0.01f);
    test::Uniform<float> rng_pos(0.01f, 1.0f);
    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Abs>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x_neg = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_neg}, .01f, .01f));

        auto x_pos = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_pos}, .01f, .01f));
    }
126 127
}

128
TEST(${BACKEND_NAME}, backwards_add)
129
{
130
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
131 132
    auto backend = manager->allocate_backend();

133
    test::Uniform<float> rng(-1.0f, 1.0f);
134
    auto shape = Shape{2, 3};
135 136
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
137

138 139
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
Scott Cyphers's avatar
Scott Cyphers committed
140 141 142
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            X0 + X1, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
143
    };
144 145
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
146 147
}

148
TEST(${BACKEND_NAME}, backwards_add_nested)
149
{
150
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
151 152
    auto backend = manager->allocate_backend();

153
    test::Uniform<float> rng(-1.0f, 1.0f);
154
    auto shape = Shape{2, 3};
155 156
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
157 158 159 160 161

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
Adam Procter's avatar
Adam Procter committed
162
            (X0 + X1) + (X1 + X0), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
163
    };
164 165
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
166 167
}

168
TEST(${BACKEND_NAME}, backwards_broadcast0)
169
{
170
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
171 172
    auto backend = manager->allocate_backend();

173
    test::Uniform<float> rng(-1.0f, 1.0f);
174
    auto shape = Shape{3};
175
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
176 177 178 179 180 181 182

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Broadcast>(X0, Shape{2, 3}, AxisSet{0}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
183
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
184 185
}

186
TEST(${BACKEND_NAME}, backwards_broadcast1)
187
{
188
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
189 190
    auto backend = manager->allocate_backend();

191
    test::Uniform<float> rng(-1.0f, 1.0f);
192
    auto shape = Shape{3};
193
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
194 195 196 197 198 199 200

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Broadcast>(X0, Shape{3, 2}, AxisSet{1}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
201
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
202 203
}

204
TEST(${BACKEND_NAME}, backwards_concat_vector)
205
{
206
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape_0 = Shape{3};
    auto x0 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_0));
    auto shape_1 = Shape{2};
    auto x1 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_1));
    auto shape_2 = Shape{1};
    auto x2 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_2));

    auto make_graph = [shape_0, shape_1, shape_2]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape_0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape_1);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape_2);
        return make_shared<Function>(make_shared<op::Concat>(Nodes{X0, X1, X2}, 0),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1, x2}, .01f, .01f));
}

232
TEST(${BACKEND_NAME}, backwards_concat_axis_0)
233
{
234
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape_0 = Shape{3, 2};
    auto x0 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_0));
    auto shape_1 = Shape{2, 2};
    auto x1 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_1));
    auto shape_2 = Shape{1, 2};
    auto x2 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_2));

    auto make_graph = [shape_0, shape_1, shape_2]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape_0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape_1);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape_2);
        return make_shared<Function>(make_shared<op::Concat>(Nodes{X0, X1, X2}, 0),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1, x2}, .01f, .01f));
}

260
TEST(${BACKEND_NAME}, backwards_concat_axis_1)
261
{
262
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape_0 = Shape{2, 3};
    auto x0 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_0));
    auto shape_1 = Shape{2, 2};
    auto x1 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_1));
    auto shape_2 = Shape{2, 1};
    auto x2 = rng.initialize(
        backend->make_primary_tensor_view(element::Float32::element_type(), shape_2));

    auto make_graph = [shape_0, shape_1, shape_2]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape_0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape_1);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape_2);
        return make_shared<Function>(make_shared<op::Concat>(Nodes{X0, X1, X2}, 1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1, x2}, .01f, .01f));
}

288
TEST(${BACKEND_NAME}, backwards_ceiling)
289
{
290
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    // The numeric derivative and the symbolic one may disagree near integers, so we will dance around
    // them.
    test::Uniform<float> rng_minusone(-0.95f, -0.05f);
    test::Uniform<float> rng_plusone(0.05f, 0.95f);
    test::Uniform<float> rng_plustwo(1.05f, 1.95f);
    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Ceiling>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x_minusone = rng_minusone.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(
            manager, backend, make_graph, {x_minusone}, .01f, .01f));

        auto x_plusone = rng_plusone.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_plusone}, .01f, .01f));

        auto x_plustwo = rng_plustwo.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_plustwo}, .01f, .01f));
    }
}

325
TEST(${BACKEND_NAME}, backwards_cos)
326
{
327
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Cos>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

346
TEST(${BACKEND_NAME}, backwards_cosh)
347
{
348
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Cosh>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

367
TEST(${BACKEND_NAME}, backwards_divide)
368
{
369
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
370
    auto backend = manager->allocate_backend();
371

372 373 374
    test::Uniform<float> rng(-1.0f, 1.0f);
    test::Uniform<float> rng1(1.0f, 2.0f);
    test::Uniform<float> rng2(-2.0f, -1.0f);
375
    auto shape = Shape{2, 3};
376 377 378
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng1.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x2 = rng2.initialize(backend->make_primary_tensor_view<float>(shape));
379

380 381 382
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
Scott Cyphers's avatar
Scott Cyphers committed
383
        return make_shared<Function>(
Scott Cyphers's avatar
Scott Cyphers committed
384
            X0 / X1, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
385
    };
386 387 388 389
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x2}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
390
}
391

392
TEST(${BACKEND_NAME}, backwards_dot_scalar_scalar)
393
{
394
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
395 396
    auto backend = manager->allocate_backend();

397
    test::Uniform<float> rng(-1.0f, 1.0f);
398 399
    auto shape0 = Shape{};
    auto shape1 = Shape{};
400 401
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
402 403 404 405 406 407 408 409

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
410 411
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
412 413
}

414
TEST(${BACKEND_NAME}, backwards_dot_scalar_tensor)
415
{
416
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
417 418
    auto backend = manager->allocate_backend();

419
    test::Uniform<float> rng(-1.0f, 1.0f);
420 421
    auto shape0 = Shape{};
    auto shape1 = Shape{3, 4};
422 423
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
424 425 426 427 428 429 430 431

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
432 433
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
434 435
}

436
TEST(${BACKEND_NAME}, backwards_dot_tensor_scalar)
437
{
438
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
439 440
    auto backend = manager->allocate_backend();

441
    test::Uniform<float> rng(-1.0f, 1.0f);
442 443
    auto shape0 = Shape{3, 4};
    auto shape1 = Shape{};
444 445
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
446 447 448 449 450 451 452 453

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
454 455
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
456 457
}

458
TEST(${BACKEND_NAME}, backwards_dot_vector_vector)
459
{
460
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
461 462
    auto backend = manager->allocate_backend();

463
    test::Uniform<float> rng(-1.0f, 1.0f);
464 465
    auto shape0 = Shape{3};
    auto shape1 = Shape{3};
466 467
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
468 469 470 471 472 473 474 475

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
476 477
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
478 479
}

480
TEST(${BACKEND_NAME}, backwards_dot_tensor_vector)
481
{
482
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
483 484
    auto backend = manager->allocate_backend();

485
    test::Uniform<float> rng(-1.0f, 1.0f);
486 487
    auto shape0 = Shape{4, 3};
    auto shape1 = Shape{3};
488 489
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
490 491 492 493 494 495 496 497

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
498 499
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
500 501
}

502
TEST(${BACKEND_NAME}, backwards_dot_tensor2_tensor2)
503
{
504
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
505 506
    auto backend = manager->allocate_backend();

507
    test::Uniform<float> rng(-1.0f, 1.0f);
508 509
    auto shape0 = Shape{4, 3};
    auto shape1 = Shape{3, 5};
510 511
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
512 513 514 515 516 517 518

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
Adam Procter's avatar
Adam Procter committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
    };
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
}

TEST(${BACKEND_NAME}, backwards_dot_tensor3_tensor3)
{
    auto manager = runtime::Manager::get("NGVM");
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape0 = Shape{2, 4, 3};
    auto shape1 = Shape{4, 3, 3};
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));

    auto make_graph = [shape0, shape1]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape0);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape1);
        return make_shared<Function>(make_shared<op::Dot>(X0, X1, 2),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
541
    };
542 543
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
544 545
}

546
TEST(${BACKEND_NAME}, backwards_exp)
Scott Cyphers's avatar
Scott Cyphers committed
547
{
548
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
549 550
    auto backend = manager->allocate_backend();

551
    test::Uniform<float> rng(-1.0f, 1.0f);
Scott Cyphers's avatar
Scott Cyphers committed
552
    auto shape = Shape{2, 3};
553
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
554 555 556 557 558 559

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Exp>(X0), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
560
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
561 562
}

563
TEST(${BACKEND_NAME}, backwards_floor)
564
{
565
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
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
    auto backend = manager->allocate_backend();

    // The numeric derivative and the symbolic one may disagree near integers, so we will dance around
    // them.
    test::Uniform<float> rng_minusone(-0.95f, -0.05f);
    test::Uniform<float> rng_plusone(0.05f, 0.95f);
    test::Uniform<float> rng_plustwo(1.05f, 1.95f);
    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Floor>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x_minusone = rng_minusone.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(
            manager, backend, make_graph, {x_minusone}, .01f, .01f));

        auto x_plusone = rng_plusone.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_plusone}, .01f, .01f));

        auto x_plustwo = rng_plustwo.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_plustwo}, .01f, .01f));
    }
}

600
TEST(${BACKEND_NAME}, backwards_log)
Scott Cyphers's avatar
Scott Cyphers committed
601
{
602
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
603 604
    auto backend = manager->allocate_backend();

605
    test::Uniform<float> rng(1.0f, 2.0f);
Scott Cyphers's avatar
Scott Cyphers committed
606
    auto shape = Shape{2, 3};
607
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
608 609 610 611 612 613

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Log>(X0), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
614
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
615 616
}

617
TEST(${BACKEND_NAME}, backwards_maximum)
618
{
619
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
620 621
    auto backend = manager->allocate_backend();

622
    test::Uniform<float> rng(-1.0f, 1.0f);
623
    auto shape = Shape{2, 3};
624 625
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
626 627 628 629 630 631 632 633

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Maximum>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
634 635
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
636 637
}

638
TEST(${BACKEND_NAME}, backwards_minimum)
639
{
640
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
641 642
    auto backend = manager->allocate_backend();

643
    test::Uniform<float> rng(-1.0f, 1.0f);
644
    auto shape = Shape{2, 3};
645 646
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
647 648 649 650 651 652 653 654

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Minimum>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
655 656
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
657 658
}

659
TEST(${BACKEND_NAME}, backwards_multiply)
660
{
661
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
662
    auto backend = manager->allocate_backend();
663

664
    test::Uniform<float> rng(-1.0f, 1.0f);
665
    auto shape = Shape{2, 3};
666 667
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
668

669 670 671 672 673 674
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            X0 * X1, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
675 676
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
677
}
Scott Cyphers's avatar
Scott Cyphers committed
678

679
TEST(${BACKEND_NAME}, backwards_negative)
Scott Cyphers's avatar
Scott Cyphers committed
680
{
681
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
682 683
    auto backend = manager->allocate_backend();

684
    test::Uniform<float> rng(-1.0f, 1.0f);
Scott Cyphers's avatar
Scott Cyphers committed
685
    auto shape = Shape{2, 3};
686
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
687 688 689 690 691

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(-X0, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
692
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
693 694
}

695
TEST(${BACKEND_NAME}, backwards_parameter)
Scott Cyphers's avatar
Scott Cyphers committed
696
{
697
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
698 699
    auto backend = manager->allocate_backend();

700
    test::Uniform<float> rng(-1.0f, 1.0f);
Scott Cyphers's avatar
Scott Cyphers committed
701
    auto shape = Shape{2, 3};
702
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
703 704 705 706
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(X0, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
707
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
708 709
}

710
TEST(${BACKEND_NAME}, backwards_power)
711
{
712
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng_neg(-5.0f, -0.5f);
    test::Uniform<float> rng_pos(0.5f, 5.0f);
    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(std::make_shared<op::Power>(X0, X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };

    auto x0 = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));

    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));

    x0 = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));
    x1 = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));

    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));

    x0 = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));
    x1 = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));

    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));

    x0 = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));
    x1 = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));

    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
}

752
TEST(${BACKEND_NAME}, backwards_replace_slice)
753
{
754
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape_x = Shape{5, 5};
    auto shape_y = Shape{2, 2};
    auto make_graph = [shape_x, shape_y]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape_x);
        auto Y = make_shared<op::Parameter>(element::Float32::element_type(), shape_y);
        return make_shared<Function>(
            make_shared<op::ReplaceSlice>(X, Y, Coordinate{2, 3}, Coordinate{4, 5}),
            nullptr,
            std::vector<std::shared_ptr<op::Parameter>>{X, Y});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape_x));
        auto y = rng.initialize(backend->make_primary_tensor_view<float>(shape_y));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x, y}, .01f, .01f));
    }
}

779
TEST(${BACKEND_NAME}, backwards_reshape)
780
{
781
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
782 783
    auto backend = manager->allocate_backend();

784
    test::Uniform<float> rng(-1.0f, 1.0f);
785
    auto shape = Shape{3, 4};
786
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
787 788 789 790 791 792 793

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Reshape>(X0, AxisVector{1, 0}, Shape{4, 3}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
794
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
795 796
}

797
TEST(${BACKEND_NAME}, backwards_select)
798
{
799
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Bool::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Select>(X0, X1, X2),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x0 = backend->make_primary_tensor_view(element::Bool::element_type(), shape);
        x0->write(vector<char>{0, 1, 0, 1, 0, 1});
        auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
        auto x2 = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare_selective<float>(manager,
                                                      backend,
                                                      make_graph,
                                                      {x0, x1, x2},
                                                      .01f,
                                                      .01f,
                                                      std::vector<bool>{false, true, true}));
    }
}

831
TEST(${BACKEND_NAME}, backwards_select_nested)
832
{
833
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Bool::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Select>(X0, X1 + X2, X2 - X1),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x0 = backend->make_primary_tensor_view(element::Bool::element_type(), shape);
        x0->write(vector<char>{0, 1, 0, 1, 0, 1});
        auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
        auto x2 = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare_selective<float>(manager,
                                                      backend,
                                                      make_graph,
                                                      {x0, x1, x2},
                                                      .01f,
                                                      .01f,
                                                      std::vector<bool>{false, true, true}));
    }
}

865
TEST(${BACKEND_NAME}, backwards_sign)
866
{
867
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    auto backend = manager->allocate_backend();

    // The numeric derivative and the symbolic one may disagree around 0, so we will dance around
    // that point by skipping (-0.01,0.01).
    test::Uniform<float> rng_neg(-1.0f, -0.01f);
    test::Uniform<float> rng_pos(0.01f, 1.0f);
    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Sign>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x_neg = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_neg}, .01f, .01f));

        auto x_pos = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_pos}, .01f, .01f));
    }
}

896
TEST(${BACKEND_NAME}, backwards_sin)
897
{
898
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Sin>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

917
TEST(${BACKEND_NAME}, backwards_sinh)
918
{
919
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Sinh>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

938
TEST(${BACKEND_NAME}, backwards_slice)
939
{
940
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{5, 5};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Slice>(X, Coordinate{2, 3}, Coordinate{4, 5}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

960
TEST(${BACKEND_NAME}, backwards_sqrt)
961
{
962
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    auto backend = manager->allocate_backend();

    // Deriv has an asymptote at 0 so we'll stay away from there.
    test::Uniform<float> rng(0.1f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Sqrt>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

982
TEST(${BACKEND_NAME}, backwards_subtract)
Scott Cyphers's avatar
Scott Cyphers committed
983
{
984
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
985 986
    auto backend = manager->allocate_backend();

987
    test::Uniform<float> rng(-1.0f, 1.0f);
Scott Cyphers's avatar
Scott Cyphers committed
988
    auto shape = Shape{2, 3};
989 990
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
991 992 993 994 995 996 997

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            X0 - X1, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
998 999
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
1000
}
Scott Cyphers's avatar
Scott Cyphers committed
1001

1002
TEST(${BACKEND_NAME}, backwards_sum_v2s)
1003
{
1004
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape = Shape{8};
    auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1020
TEST(${BACKEND_NAME}, backwards_sum_m2s)
1021
{
1022
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape = Shape{8, 9};
    auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0, 1}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1038
TEST(${BACKEND_NAME}, backwards_sum_m2v_0)
1039
{
1040
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape = Shape{8, 9};
    auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1056
TEST(${BACKEND_NAME}, backwards_sum_m2v_1)
1057
{
1058
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
    auto shape = Shape{8, 9};
    auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{1}),
                                     nullptr,
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1074
TEST(${BACKEND_NAME}, backwards_tan)
1075
{
1076
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    auto backend = manager->allocate_backend();

    auto pi = 3.14159f;

    // Stay away from the asymptotes at 6 and 12 o'clock.
    auto slop = 0.1f;
    test::Uniform<float> rng_r(-pi / 2 + slop, pi / 2 - slop);
    test::Uniform<float> rng_l(pi / 2 + slop, (3 * pi) / 2 - slop);

    auto shape = Shape{2, 3};

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Tan>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x_r = rng_r.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_r}, .01f, .01f));

        auto x_l = rng_l.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(
            autodiff_numeric_compare<float>(manager, backend, make_graph, {x_l}, .01f, .01f));
    }
}

1108
TEST(${BACKEND_NAME}, backwards_tanh)
1109
{
1110
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
    auto shape = Shape{2, 3};
    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            make_shared<op::Tanh>(X), nullptr, std::vector<std::shared_ptr<op::Parameter>>{X});
    };

    for (auto i = 0; i < 100; i++)
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

1129
TEST(${BACKEND_NAME}, backwards_abc)
Scott Cyphers's avatar
Scott Cyphers committed
1130
{
1131
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
1132 1133
    auto backend = manager->allocate_backend();

1134
    test::Uniform<float> rng(-1.0f, 1.0f);
Scott Cyphers's avatar
Scott Cyphers committed
1135
    auto shape = Shape{2, 3};
1136 1137 1138
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x2 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
1139 1140 1141 1142 1143 1144 1145 1146

    auto make_graph = [shape]() {
        auto X0 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X1 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        auto X2 = make_shared<op::Parameter>(element::Float32::element_type(), shape);
        return make_shared<Function>(
            (X0 + X1) * X2, nullptr, std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };
1147 1148
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1, x2}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
1149
}