autodiff.in.cpp 61.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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
* limitations under the License.
*******************************************************************************/
16 17

#include <algorithm>
18
#include <functional>
19
#include <memory>
20
#include <tuple>
21 22 23 24

#include "gtest/gtest.h"

#include "ngraph/ngraph.hpp"
25
#include "util/autodiff/backprop_function.hpp"
adstraw's avatar
adstraw committed
26
#include "util/autodiff/numeric_compare.hpp"
27
#include "util/random.hpp"
28

29 30
#include "ngraph/runtime/kernel/avg_pool.hpp"

31 32 33
using namespace std;
using namespace ngraph;

34 35
TEST(${BACKEND_NAME}, backwards_maxpool_n4_c1_hw4_2x2_max)
{
fenglei.tian's avatar
fenglei.tian committed
36
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
37 38 39
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

40 41
    Shape shape_a{1, 4, 4, 4}; //in CHWN
    Shape maxpool_shape{1, 4, 3, 3};
42 43 44 45

    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    auto reshape = make_shared<op::Reshape>(
        A, AxisVector{0, 3, 1, 2}, Shape{1, 4, 4, 4}); //convert CHWN to CNHW
46
    Shape window_shape{2, 2};
47 48
    auto window_movement_strides = Strides{1, 1};
    auto maxpool = make_shared<op::MaxPool>(reshape, window_shape, window_movement_strides);
49
    auto f = make_shared<Function>(maxpool, op::ParameterVector{A});
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

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::i32, maxpool_shape);
    vector<int> dataEp(shape_size(maxpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::i32, shape_a);
    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::i32, shape_a);

    vector<int> dataInput{11, 65, 44, 28, 31, 33, 21, 66, 40, 49, 69, 57, 47, 30, 24, 27,
                          13, 56, 46, 60, 61, 41, 25, 42, 48, 53, 51, 43, 59, 58, 29, 71,
                          17, 22, 72, 18, 39, 35, 15, 38, 64, 52, 73, 67, 62, 50, 10, 68,
                          45, 63, 16, 14, 55, 54, 37, 20, 36, 12, 70, 34, 19, 26, 32, 23};

    vector<int> expected{//delta
                         0, 4, 0, 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, 0, 0, 0, 0, 4, 4,  4, 12, 0,
                         0, 0, 0, 8, 0, 0, 4, 8, 0, 8, 0, 0, 8, 0, 0, 0, 0, 4, 16, 4, 16, 8,
                         0, 0, 0, 4, 0, 4, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0};

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::i32, maxpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
78
    ASSERT_TRUE(read_vector<int>(output) == expected);
79 80 81 82
}

TEST(${BACKEND_NAME}, backwards_maxpool_n2_c1_hw5_3x3_str2_max)
{
fenglei.tian's avatar
fenglei.tian committed
83
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
84 85 86
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

87 88
    Shape shape_a{1, 5, 5, 2}; //in CHWN
    Shape maxpool_shape{1, 2, 2, 2};
89 90 91 92

    auto A = make_shared<op::Parameter>(element::i32, shape_a);
    auto reshape = make_shared<op::Reshape>(
        A, AxisVector{0, 3, 1, 2}, Shape{1, 2, 5, 5}); //convert CHWN to CNHW
93
    Shape window_shape{3, 3};
94 95
    auto window_movement_strides = Strides{2, 2};
    auto maxpool = make_shared<op::MaxPool>(reshape, window_shape, window_movement_strides);
96
    auto f = make_shared<Function>(maxpool, op::ParameterVector{A});
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

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::i32, maxpool_shape);
    vector<int> dataEp(shape_size(maxpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::i32, shape_a);
    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::i32, shape_a);

    vector<int> dataInput{58, 15, 51, 35, 18, 47, 31, 32, 52, 21, 36, 38, 57, 54, 25, 45, 23,
                          30, 16, 27, 48, 20, 41, 37, 43, 39, 22, 28, 33, 29, 12, 17, 44, 42,
                          19, 40, 10, 46, 34, 53, 26, 55, 50, 13, 24, 14, 49, 56, 59, 11};

    vector<int> expected{//delta
                         4, 0, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 4, 4, 0};

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::i32, maxpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
123
    ASSERT_TRUE(read_vector<int>(output) == expected);
124 125
}

126 127
TEST(${BACKEND_NAME}, backwards_avgpool_n1_c1_hw2x2)
{
fenglei.tian's avatar
fenglei.tian committed
128
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
129 130 131
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

132
    Shape padding{1, 1};
133

134 135
    Shape shape_a{1, 1, 2, 2};
    Shape avgpool_shape{1, 1, 2, 2};
136 137

    auto A = make_shared<op::Parameter>(element::i32, shape_a);
138
    Shape window_shape{2, 2};
139 140
    auto window_movement_strides = Strides{2, 2};
    auto avgpool =
141
        make_shared<op::AvgPool>(A, window_shape, window_movement_strides, padding, padding, false);
142
    auto f = make_shared<Function>(avgpool, op::ParameterVector{A});
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

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::i32, avgpool_shape);
    vector<int> dataEp(shape_size(avgpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::i32, shape_a);

    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::i32, shape_a);

    vector<int> dataInput{4, 8, 12, 16};

    vector<int> expected{1, 2, 3, 4};

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::i32, avgpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
    ASSERT_TRUE(read_vector<int>(output) == dataEp);
}

TEST(${BACKEND_NAME}, backwards_avgpool_n1_c1_hw4x4)
{
fenglei.tian's avatar
fenglei.tian committed
171
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
172 173 174
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

175 176
    Shape shape_a{1, 1, 4, 4};
    Shape avgpool_shape{1, 1, 3, 3};
177 178

    auto A = make_shared<op::Parameter>(element::i32, shape_a);
179
    Shape window_shape{2, 2};
180 181
    auto window_movement_strides = Strides{1, 1};
    auto avgpool = make_shared<op::AvgPool>(A, window_shape, window_movement_strides);
182
    auto f = make_shared<Function>(avgpool, op::ParameterVector{A});
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

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::i32, avgpool_shape);
    vector<int> dataEp(shape_size(avgpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::i32, shape_a);

    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::i32, shape_a);

    vector<int> dataInput{1, 3, 1, 3, 1, 3, 1, 3, 3, 5, 3, 5, 3, 5, 3, 5};

    vector<int> expected{1, 2, 2, 1, 2, 4, 4, 2, 2, 4, 4, 2, 1, 2, 2, 1};

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::i32, avgpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
    ASSERT_TRUE(read_vector<int>(output) == expected);
}

TEST(${BACKEND_NAME}, backwards_avgpool_n2_c2_hw4x4)
{
fenglei.tian's avatar
fenglei.tian committed
211
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
212 213 214
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

215 216
    Shape shape_a{2, 2, 4, 4};
    Shape avgpool_shape{2, 2, 2, 2};
217 218

    auto A = make_shared<op::Parameter>(element::i32, shape_a);
219
    Shape window_shape{2, 2};
220 221
    auto window_movement_strides = Strides{2, 2};
    auto avgpool = make_shared<op::AvgPool>(A, window_shape, window_movement_strides);
222
    auto f = make_shared<Function>(avgpool, op::ParameterVector{A});
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

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::i32, avgpool_shape);
    vector<int> dataEp(shape_size(avgpool_shape), 12);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::i32, shape_a);

    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::i32, shape_a);

    vector<int> dataInput{//i1c1
                          1,
                          2,
                          6,
                          7,
                          3,
                          4,
                          4,
                          3,
                          19,
                          1,
                          2,
                          3,
                          18,
                          2,
                          3,
                          2,
                          //i1c2
                          4,
                          1,
                          5,
                          5,
                          1,
                          4,
                          5,
                          5,
                          12,
                          8,
                          2,
                          3,
                          15,
                          5,
                          3,
                          2,
                          //i2c1
                          2,
                          3,
                          7,
                          7,
                          3,
                          2,
                          3,
                          3,
                          13,
                          7,
                          1,
                          2,
                          7,
                          13,
                          3,
                          4,
                          //i2c2
                          1,
                          1,
                          2,
                          2,
                          7,
                          1,
                          2,
                          14,
                          6,
                          16,
                          4,
                          1,
                          14,
                          4,
                          4,
                          1};

    vector<int> expected(shape_size(shape_a), 3);
    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::i32, avgpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
    ASSERT_TRUE(read_vector<int>(output) == expected);
}

TEST(${BACKEND_NAME}, backwards_avgpool_n2_c2_hw4x4_numeric)
{
fenglei.tian's avatar
fenglei.tian committed
317
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
318 319
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();
320
    Shape shape_a{2, 2, 4, 4};
321 322 323 324
    test::Uniform<float> rng(1.0f, 10.0f);

    auto make_graph = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
325
        Shape window_shape{2, 2};
326 327
        auto window_movement_strides = Strides{2, 2};
        auto avgpool = make_shared<op::AvgPool>(A, window_shape, window_movement_strides);
328
        return make_shared<Function>(avgpool, op::ParameterVector{A});
329 330 331

    };

332
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
333 334 335 336 337 338 339 340
    {
        auto x = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_a));
        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

TEST(${BACKEND_NAME}, backwards_avgpool_n2_c2_hw4x4_win_2x2_str_1x1_numeric)
{
fenglei.tian's avatar
fenglei.tian committed
341
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
342 343
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();
344
    Shape shape_a{2, 2, 4, 4};
345 346 347 348
    test::Uniform<float> rng(1.0f, 10.0f);

    auto make_graph = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
349
        Shape window_shape{2, 2};
350 351
        auto window_movement_strides = Strides{1, 1};
        auto avgpool = make_shared<op::AvgPool>(A, window_shape, window_movement_strides);
352
        return make_shared<Function>(avgpool, op::ParameterVector{A});
353 354 355

    };

356
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
357 358 359 360 361 362 363 364
    {
        auto x = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_a));
        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

TEST(${BACKEND_NAME}, backwards_avgpool_n2_c2_hw2x2_win_2x2_str_1x1_padding_numeric)
{
fenglei.tian's avatar
fenglei.tian committed
365
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
366 367
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();
368
    Shape shape_a{2, 2, 4, 4};
369 370 371 372
    test::Uniform<float> rng(1.0f, 10.0f);

    auto make_graph = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
373 374
        Shape window_shape{2, 2};
        Shape padding{1, 1};
375
        auto window_movement_strides = Strides{2, 2};
376 377
        auto avgpool = make_shared<op::AvgPool>(
            A, window_shape, window_movement_strides, padding, padding, false);
378
        return make_shared<Function>(avgpool, op::ParameterVector{A});
379 380 381

    };

382
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
383 384 385 386 387 388
    {
        auto x = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_a));
        EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
    }
}

389
TEST(${BACKEND_NAME}, backwards_abs)
390
{
fenglei.tian's avatar
fenglei.tian committed
391
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
392
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
393 394 395 396 397 398
    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);
399
    Shape shape{2, 3};
400 401

    auto make_graph = [shape]() {
402
        auto X = make_shared<op::Parameter>(element::f32, shape);
403 404
        return make_shared<Function>(make_shared<op::Abs>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
405 406
    };

407
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
408 409 410 411 412 413 414 415 416 417 418
    {
        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));
    }
419 420
}

421
TEST(${BACKEND_NAME}, backwards_add)
422
{
423
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
424 425
    auto backend = manager->allocate_backend();

426
    test::Uniform<float> rng(-1.0f, 1.0f);
427
    Shape shape{2, 3};
428 429
    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
430

431
    auto make_graph = [shape]() {
432 433
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
434
        return make_shared<Function>(X0 + X1, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
435
    };
436 437
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
438 439
}

440
TEST(${BACKEND_NAME}, backwards_add_nested)
441
{
fenglei.tian's avatar
fenglei.tian committed
442
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
443
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
444 445
    auto backend = manager->allocate_backend();

446
    test::Uniform<float> rng(-1.0f, 1.0f);
447
    Shape shape{2, 3};
448 449
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
450 451

    auto make_graph = [shape]() {
452 453
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
454 455
        return make_shared<Function>((X0 + X1) + (X1 + X0),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
456
    };
457 458
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
459 460
}

461
TEST(${BACKEND_NAME}, backwards_broadcast0)
462
{
fenglei.tian's avatar
fenglei.tian committed
463
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
464
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
465 466
    auto backend = manager->allocate_backend();

467
    test::Uniform<float> rng(-1.0f, 1.0f);
468
    Shape shape{3};
469
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
470 471

    auto make_graph = [shape]() {
472
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
473 474 475
        return make_shared<Function>(make_shared<op::Broadcast>(X0, Shape{2, 3}, AxisSet{0}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
476
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
477 478
}

479
TEST(${BACKEND_NAME}, backwards_broadcast1)
480
{
fenglei.tian's avatar
fenglei.tian committed
481
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
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
    Shape shape{3};
487
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
488 489

    auto make_graph = [shape]() {
490
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
491 492 493
        return make_shared<Function>(make_shared<op::Broadcast>(X0, Shape{3, 2}, AxisSet{1}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
494
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
495 496
}

497
TEST(${BACKEND_NAME}, backwards_concat_vector)
498
{
fenglei.tian's avatar
fenglei.tian committed
499
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
500 501
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

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

    test::Uniform<float> rng(-1.0f, 1.0f);
506
    Shape shape_0{3};
507
    auto x0 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_0));
508
    Shape shape_1{2};
509
    auto x1 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_1));
510
    Shape shape_2{1};
511
    auto x2 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_2));
512 513

    auto make_graph = [shape_0, shape_1, shape_2]() {
514 515 516
        auto X0 = make_shared<op::Parameter>(element::f32, shape_0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape_1);
        auto X2 = make_shared<op::Parameter>(element::f32, shape_2);
517
        return make_shared<Function>(make_shared<op::Concat>(NodeVector{X0, X1, X2}, 0),
518 519 520 521 522 523
                                     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));
}

524
TEST(${BACKEND_NAME}, backwards_concat_axis_0)
525
{
fenglei.tian's avatar
fenglei.tian committed
526
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
527 528
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

529
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
530 531 532
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
533
    Shape shape_0{3, 2};
534
    auto x0 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_0));
535
    Shape shape_1{2, 2};
536
    auto x1 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_1));
537
    Shape shape_2{1, 2};
538
    auto x2 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_2));
539 540

    auto make_graph = [shape_0, shape_1, shape_2]() {
541 542 543
        auto X0 = make_shared<op::Parameter>(element::f32, shape_0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape_1);
        auto X2 = make_shared<op::Parameter>(element::f32, shape_2);
544
        return make_shared<Function>(make_shared<op::Concat>(NodeVector{X0, X1, X2}, 0),
545 546 547 548 549 550
                                     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));
}

551
TEST(${BACKEND_NAME}, backwards_concat_axis_1)
552
{
fenglei.tian's avatar
fenglei.tian committed
553
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
554 555
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

556
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
557 558 559
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
560
    Shape shape_0{2, 3};
561
    auto x0 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_0));
562
    Shape shape_1{2, 2};
563
    auto x1 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_1));
564
    Shape shape_2{2, 1};
565
    auto x2 = rng.initialize(backend->make_primary_tensor_view(element::f32, shape_2));
566 567

    auto make_graph = [shape_0, shape_1, shape_2]() {
568 569 570
        auto X0 = make_shared<op::Parameter>(element::f32, shape_0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape_1);
        auto X2 = make_shared<op::Parameter>(element::f32, shape_2);
571
        return make_shared<Function>(make_shared<op::Concat>(NodeVector{X0, X1, X2}, 1),
572 573 574 575 576 577
                                     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));
}

578
TEST(${BACKEND_NAME}, backwards_ceiling)
579
{
fenglei.tian's avatar
fenglei.tian committed
580
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
581 582
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

583
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
584 585 586 587 588 589 590
    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);
591
    Shape shape{2, 3};
592 593

    auto make_graph = [shape]() {
594
        auto X = make_shared<op::Parameter>(element::f32, shape);
595 596
        return make_shared<Function>(make_shared<op::Ceiling>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
597 598
    };

599
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    {
        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));
    }
}

618
TEST(${BACKEND_NAME}, backwards_cos)
619
{
fenglei.tian's avatar
fenglei.tian committed
620
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
621
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
622 623 624
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
625
    Shape shape{2, 3};
626
    auto make_graph = [shape]() {
627
        auto X = make_shared<op::Parameter>(element::f32, shape);
628 629
        return make_shared<Function>(make_shared<op::Cos>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
630 631
    };

632
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
633 634 635 636 637 638 639
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

640
TEST(${BACKEND_NAME}, backwards_cosh)
641
{
fenglei.tian's avatar
fenglei.tian committed
642
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
643 644
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

645
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
646 647 648
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
649
    Shape shape{2, 3};
650
    auto make_graph = [shape]() {
651
        auto X = make_shared<op::Parameter>(element::f32, shape);
652 653
        return make_shared<Function>(make_shared<op::Cosh>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
654 655
    };

656
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
657 658 659 660 661 662 663
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

664
TEST(${BACKEND_NAME}, backwards_divide)
665
{
fenglei.tian's avatar
fenglei.tian committed
666
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
667 668
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

669
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
670
    auto backend = manager->allocate_backend();
671

672 673 674
    test::Uniform<float> rng(-1.0f, 1.0f);
    test::Uniform<float> rng1(1.0f, 2.0f);
    test::Uniform<float> rng2(-2.0f, -1.0f);
675
    Shape shape{2, 3};
676 677 678
    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));
679

680
    auto make_graph = [shape]() {
681 682
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
683
        return make_shared<Function>(X0 / X1, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
684
    };
685 686 687 688
    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
689
}
690

691
TEST(${BACKEND_NAME}, backwards_dot_scalar_scalar)
692
{
fenglei.tian's avatar
fenglei.tian committed
693
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
694
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
695 696
    auto backend = manager->allocate_backend();

697
    test::Uniform<float> rng(-1.0f, 1.0f);
698 699
    Shape shape0{};
    Shape shape1{};
700 701
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
702 703

    auto make_graph = [shape0, shape1]() {
704 705
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
706 707 708
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
709 710
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
711 712
}

713
TEST(${BACKEND_NAME}, backwards_dot_scalar_tensor)
714
{
fenglei.tian's avatar
fenglei.tian committed
715
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
716
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
717 718
    auto backend = manager->allocate_backend();

719
    test::Uniform<float> rng(-1.0f, 1.0f);
720 721
    Shape shape0{};
    Shape shape1{3, 4};
722 723
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
724 725

    auto make_graph = [shape0, shape1]() {
726 727
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
728 729 730
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
731 732
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
733 734
}

735
TEST(${BACKEND_NAME}, backwards_dot_tensor_scalar)
736
{
fenglei.tian's avatar
fenglei.tian committed
737
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
738
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
739 740
    auto backend = manager->allocate_backend();

741
    test::Uniform<float> rng(-1.0f, 1.0f);
742 743
    Shape shape0{3, 4};
    Shape shape1{};
744 745
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
746 747

    auto make_graph = [shape0, shape1]() {
748 749
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
750 751 752
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
753 754
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
755 756
}

757
TEST(${BACKEND_NAME}, backwards_dot_vector_vector)
758
{
fenglei.tian's avatar
fenglei.tian committed
759
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
760
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
761 762
    auto backend = manager->allocate_backend();

763
    test::Uniform<float> rng(-1.0f, 1.0f);
764 765
    Shape shape0{3};
    Shape shape1{3};
766 767
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
768 769

    auto make_graph = [shape0, shape1]() {
770 771
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
772 773 774
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
775 776
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
777 778
}

779
TEST(${BACKEND_NAME}, backwards_dot_tensor_vector)
780
{
fenglei.tian's avatar
fenglei.tian committed
781
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
782
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
783 784
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape0, shape1]() {
792 793
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
794 795 796
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
797 798
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
799 800
}

801
TEST(${BACKEND_NAME}, backwards_dot_tensor2_tensor2)
802
{
fenglei.tian's avatar
fenglei.tian committed
803
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
804
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
805 806
    auto backend = manager->allocate_backend();

807
    test::Uniform<float> rng(-1.0f, 1.0f);
808 809
    Shape shape0{4, 3};
    Shape shape1{3, 5};
810 811
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape0));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape1));
812 813

    auto make_graph = [shape0, shape1]() {
814 815
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
816 817
        return make_shared<Function>(make_shared<op::Dot>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
Adam Procter's avatar
Adam Procter committed
818 819 820 821 822 823 824
    };
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
}

TEST(${BACKEND_NAME}, backwards_dot_tensor3_tensor3)
{
fenglei.tian's avatar
fenglei.tian committed
825
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
826
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Adam Procter's avatar
Adam Procter committed
827 828 829
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
830 831
    Shape shape0{2, 4, 3};
    Shape shape1{4, 3, 3};
Adam Procter's avatar
Adam Procter committed
832 833 834 835
    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]() {
836 837
        auto X0 = make_shared<op::Parameter>(element::f32, shape0);
        auto X1 = make_shared<op::Parameter>(element::f32, shape1);
Adam Procter's avatar
Adam Procter committed
838 839
        return make_shared<Function>(make_shared<op::Dot>(X0, X1, 2),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
840
    };
841 842
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
843 844
}

845
TEST(${BACKEND_NAME}, backwards_exp)
Scott Cyphers's avatar
Scott Cyphers committed
846
{
fenglei.tian's avatar
fenglei.tian committed
847
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
848
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
849 850
    auto backend = manager->allocate_backend();

851
    test::Uniform<float> rng(-1.0f, 1.0f);
852
    Shape shape{2, 3};
853
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
854 855

    auto make_graph = [shape]() {
856
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
857 858
        return make_shared<Function>(make_shared<op::Exp>(X0),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
Scott Cyphers's avatar
Scott Cyphers committed
859
    };
860
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
861 862
}

863
TEST(${BACKEND_NAME}, backwards_floor)
864
{
fenglei.tian's avatar
fenglei.tian committed
865
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
866 867
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

868
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
869 870 871 872 873 874 875
    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);
876
    Shape shape{2, 3};
877 878

    auto make_graph = [shape]() {
879
        auto X = make_shared<op::Parameter>(element::f32, shape);
880 881
        return make_shared<Function>(make_shared<op::Floor>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
882 883
    };

884
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    {
        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));
    }
}

903
TEST(${BACKEND_NAME}, backwards_log)
Scott Cyphers's avatar
Scott Cyphers committed
904
{
fenglei.tian's avatar
fenglei.tian committed
905
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
906
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
907 908
    auto backend = manager->allocate_backend();

909
    test::Uniform<float> rng(1.0f, 2.0f);
910
    Shape shape{2, 3};
911
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
912 913

    auto make_graph = [shape]() {
914
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
915 916
        return make_shared<Function>(make_shared<op::Log>(X0),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
Scott Cyphers's avatar
Scott Cyphers committed
917
    };
918
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
919 920
}

921
TEST(${BACKEND_NAME}, backwards_maximum)
922
{
fenglei.tian's avatar
fenglei.tian committed
923
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
924
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
925 926
    auto backend = manager->allocate_backend();

927
    test::Uniform<float> rng(-1.0f, 1.0f);
928
    Shape shape{2, 3};
929 930
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
931 932

    auto make_graph = [shape]() {
933 934
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
935 936 937
        return make_shared<Function>(make_shared<op::Maximum>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
938 939
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
940 941
}

942
TEST(${BACKEND_NAME}, backwards_minimum)
943
{
fenglei.tian's avatar
fenglei.tian committed
944
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
945
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
946 947
    auto backend = manager->allocate_backend();

948
    test::Uniform<float> rng(-1.0f, 1.0f);
949
    Shape shape{2, 3};
950 951
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
952 953

    auto make_graph = [shape]() {
954 955
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
956 957 958
        return make_shared<Function>(make_shared<op::Minimum>(X0, X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
    };
959 960
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
961 962
}

963
TEST(${BACKEND_NAME}, backwards_multiply)
964
{
965
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
966
    auto backend = manager->allocate_backend();
967

968
    test::Uniform<float> rng(-1.0f, 1.0f);
969
    Shape shape{2, 3};
970 971
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
972

973
    auto make_graph = [shape]() {
974 975
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
976
        return make_shared<Function>(X0 * X1, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
977
    };
978 979
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
980
}
Scott Cyphers's avatar
Scott Cyphers committed
981

982
TEST(${BACKEND_NAME}, backwards_negative)
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);
988
    Shape shape{2, 3};
989
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
990 991

    auto make_graph = [shape]() {
992
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
993
        return make_shared<Function>(-X0, std::vector<std::shared_ptr<op::Parameter>>{X0});
Scott Cyphers's avatar
Scott Cyphers committed
994
    };
995
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
996 997
}

998
TEST(${BACKEND_NAME}, backwards_parameter)
Scott Cyphers's avatar
Scott Cyphers committed
999
{
fenglei.tian's avatar
fenglei.tian committed
1000
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1001
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
1002 1003
    auto backend = manager->allocate_backend();

1004
    test::Uniform<float> rng(-1.0f, 1.0f);
1005
    Shape shape{2, 3};
1006
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
Scott Cyphers's avatar
Scott Cyphers committed
1007
    auto make_graph = [shape]() {
1008
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
1009
        return make_shared<Function>(X0, std::vector<std::shared_ptr<op::Parameter>>{X0});
Scott Cyphers's avatar
Scott Cyphers committed
1010
    };
1011
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
1012 1013
}

1014
TEST(${BACKEND_NAME}, backwards_power)
1015
{
fenglei.tian's avatar
fenglei.tian committed
1016
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1017 1018
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1019
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1020 1021 1022 1023
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape]() {
1027 1028
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        return make_shared<Function>(std::make_shared<op::Power>(X0, X1),
                                     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_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));
}

1052 1053
TEST(${BACKEND_NAME}, backwards_relu)
{
fenglei.tian's avatar
fenglei.tian committed
1054
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng_neg(-1.0f, -0.01f);
    test::Uniform<float> rng_pos(0.01f, 1.0f);
    Shape shape{2, 3};
    auto x0 = rng_neg.initialize(backend->make_primary_tensor_view<float>(shape));
    auto x1 = rng_pos.initialize(backend->make_primary_tensor_view<float>(shape));

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

    for (auto i = 0; i < ${TEST_LOOPS}; 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));
    }
}

1084
TEST(${BACKEND_NAME}, backwards_replace_slice)
1085
{
fenglei.tian's avatar
fenglei.tian committed
1086
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1087 1088
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1089
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1090 1091 1092
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1093 1094
    Shape shape_x{5, 5};
    Shape shape_y{2, 2};
1095
    auto make_graph = [shape_x, shape_y]() {
1096 1097
        auto X = make_shared<op::Parameter>(element::f32, shape_x);
        auto Y = make_shared<op::Parameter>(element::f32, shape_y);
1098 1099 1100 1101 1102
        return make_shared<Function>(
            make_shared<op::ReplaceSlice>(X, Y, Coordinate{2, 3}, Coordinate{4, 5}),
            std::vector<std::shared_ptr<op::Parameter>>{X, Y});
    };

1103
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1104 1105 1106 1107 1108 1109 1110 1111 1112
    {
        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));
    }
}

1113
TEST(${BACKEND_NAME}, backwards_reshape)
1114
{
fenglei.tian's avatar
fenglei.tian committed
1115
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1116
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1117 1118
    auto backend = manager->allocate_backend();

1119
    test::Uniform<float> rng(-1.0f, 1.0f);
1120
    Shape shape{3, 4};
1121
    auto x0 = rng.initialize(backend->make_primary_tensor_view<float>(shape));
1122 1123

    auto make_graph = [shape]() {
1124
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
1125 1126 1127
        return make_shared<Function>(make_shared<op::Reshape>(X0, AxisVector{1, 0}, Shape{4, 3}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0});
    };
1128
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x0}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
1129 1130
}

1131
TEST(${BACKEND_NAME}, backwards_select)
1132
{
fenglei.tian's avatar
fenglei.tian committed
1133
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1134 1135
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1136
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1137 1138 1139
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1140
    Shape shape{2, 3};
1141
    auto make_graph = [shape]() {
1142 1143 1144
        auto X0 = make_shared<op::Parameter>(element::boolean, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
        auto X2 = make_shared<op::Parameter>(element::f32, shape);
1145 1146 1147 1148
        return make_shared<Function>(make_shared<op::Select>(X0, X1, X2),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };

1149
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1150
    {
1151
        auto x0 = backend->make_primary_tensor_view(element::boolean, shape);
1152
        write_vector(x0, vector<char>{0, 1, 0, 1, 0, 1});
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
        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}));
    }
}

1167
TEST(${BACKEND_NAME}, backwards_select_nested)
1168
{
fenglei.tian's avatar
fenglei.tian committed
1169
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1170 1171
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1172
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1173 1174 1175
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1176
    Shape shape{2, 3};
1177
    auto make_graph = [shape]() {
1178 1179 1180
        auto X0 = make_shared<op::Parameter>(element::boolean, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
        auto X2 = make_shared<op::Parameter>(element::f32, shape);
1181 1182 1183 1184
        return make_shared<Function>(make_shared<op::Select>(X0, X1 + X2, X2 - X1),
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
    };

1185
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1186
    {
1187
        auto x0 = backend->make_primary_tensor_view(element::boolean, shape);
1188
        write_vector(x0, vector<char>{0, 1, 0, 1, 0, 1});
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
        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}));
    }
}

1203
TEST(${BACKEND_NAME}, backwards_sign)
1204
{
fenglei.tian's avatar
fenglei.tian committed
1205
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1206 1207
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1208
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1209 1210 1211 1212 1213 1214
    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);
1215
    Shape shape{2, 3};
1216 1217

    auto make_graph = [shape]() {
1218
        auto X = make_shared<op::Parameter>(element::f32, shape);
1219 1220
        return make_shared<Function>(make_shared<op::Sign>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1221 1222
    };

1223
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    {
        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));
    }
}

1237
TEST(${BACKEND_NAME}, backwards_sin)
1238
{
fenglei.tian's avatar
fenglei.tian committed
1239
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1240
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1241 1242 1243
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1244
    Shape shape{2, 3};
1245
    auto make_graph = [shape]() {
1246
        auto X = make_shared<op::Parameter>(element::f32, shape);
1247 1248
        return make_shared<Function>(make_shared<op::Sin>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1249 1250
    };

1251
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1252 1253 1254 1255 1256 1257 1258
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

1259
TEST(${BACKEND_NAME}, backwards_sinh)
1260
{
fenglei.tian's avatar
fenglei.tian committed
1261
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1262 1263
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1264
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1265 1266 1267
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1268
    Shape shape{2, 3};
1269
    auto make_graph = [shape]() {
1270
        auto X = make_shared<op::Parameter>(element::f32, shape);
1271 1272
        return make_shared<Function>(make_shared<op::Sinh>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1273 1274
    };

1275
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1276 1277 1278 1279 1280 1281 1282
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

1283
TEST(${BACKEND_NAME}, backwards_slice)
1284
{
fenglei.tian's avatar
fenglei.tian committed
1285
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1286 1287
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1288
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1289 1290
    auto backend = manager->allocate_backend();
    test::Uniform<float> rng(-10.0f, 10.0f);
1291
    Shape shape{5, 5};
1292
    auto make_graph = [shape]() {
1293
        auto X = make_shared<op::Parameter>(element::f32, shape);
1294 1295 1296 1297
        return make_shared<Function>(make_shared<op::Slice>(X, Coordinate{2, 3}, Coordinate{4, 5}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };

1298
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1299 1300 1301 1302 1303 1304 1305
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

1306
TEST(${BACKEND_NAME}, backwards_sqrt)
1307
{
fenglei.tian's avatar
fenglei.tian committed
1308
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1309
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1310 1311 1312 1313
    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);
1314
    Shape shape{2, 3};
1315
    auto make_graph = [shape]() {
1316
        auto X = make_shared<op::Parameter>(element::f32, shape);
1317 1318
        return make_shared<Function>(make_shared<op::Sqrt>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1319 1320
    };

1321
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1322 1323 1324 1325 1326 1327 1328
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

1329
TEST(${BACKEND_NAME}, backwards_subtract)
Scott Cyphers's avatar
Scott Cyphers committed
1330
{
fenglei.tian's avatar
fenglei.tian committed
1331
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1332
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
1333 1334
    auto backend = manager->allocate_backend();

1335
    test::Uniform<float> rng(-1.0f, 1.0f);
1336
    Shape shape{2, 3};
1337 1338
    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
1339 1340

    auto make_graph = [shape]() {
1341 1342
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
1343
        return make_shared<Function>(X0 - X1, std::vector<std::shared_ptr<op::Parameter>>{X0, X1});
Scott Cyphers's avatar
Scott Cyphers committed
1344
    };
1345 1346
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
1347
}
Scott Cyphers's avatar
Scott Cyphers committed
1348

1349
TEST(${BACKEND_NAME}, backwards_sum_v2s)
1350
{
fenglei.tian's avatar
fenglei.tian committed
1351
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1352
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1353 1354 1355
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape]() {
1360
        auto X = make_shared<op::Parameter>(element::f32, shape);
1361 1362 1363 1364 1365 1366
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1367
TEST(${BACKEND_NAME}, backwards_sum_m2s)
1368
{
fenglei.tian's avatar
fenglei.tian committed
1369
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1370
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1371 1372 1373
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape]() {
1378
        auto X = make_shared<op::Parameter>(element::f32, shape);
1379 1380 1381 1382 1383 1384
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0, 1}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1385
TEST(${BACKEND_NAME}, backwards_sum_m2v_0)
1386
{
fenglei.tian's avatar
fenglei.tian committed
1387
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1388
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1389 1390 1391
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape]() {
1396
        auto X = make_shared<op::Parameter>(element::f32, shape);
1397 1398 1399 1400 1401 1402
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{0}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1403
TEST(${BACKEND_NAME}, backwards_sum_m2v_1)
1404
{
fenglei.tian's avatar
fenglei.tian committed
1405
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1406
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1407 1408 1409
    auto backend = manager->allocate_backend();

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

    auto make_graph = [shape]() {
1414
        auto X = make_shared<op::Parameter>(element::f32, shape);
1415 1416 1417 1418 1419 1420
        return make_shared<Function>(make_shared<op::Sum>(X, AxisSet{1}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}

1421
TEST(${BACKEND_NAME}, backwards_tan)
1422
{
fenglei.tian's avatar
fenglei.tian committed
1423
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1424 1425
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1426
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1427 1428 1429 1430 1431 1432 1433 1434 1435
    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);

1436
    Shape shape{2, 3};
1437 1438

    auto make_graph = [shape]() {
1439
        auto X = make_shared<op::Parameter>(element::f32, shape);
1440 1441
        return make_shared<Function>(make_shared<op::Tan>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1442 1443
    };

1444
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    {
        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));
    }
}

1458
TEST(${BACKEND_NAME}, backwards_tanh)
1459
{
fenglei.tian's avatar
fenglei.tian committed
1460
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1461 1462
    SKIP_TEST_FOR("ARGON", "${BACKEND_NAME}");

1463
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
1464 1465 1466
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-10.0f, 10.0f);
1467
    Shape shape{2, 3};
1468
    auto make_graph = [shape]() {
1469
        auto X = make_shared<op::Parameter>(element::f32, shape);
1470 1471
        return make_shared<Function>(make_shared<op::Tanh>(X),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
1472 1473
    };

1474
    for (auto i = 0; i < ${TEST_LOOPS}; i++)
1475 1476 1477 1478 1479 1480 1481
    {
        auto x = rng.initialize(backend->make_primary_tensor_view<float>(shape));

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

1482
TEST(${BACKEND_NAME}, backwards_abc)
Scott Cyphers's avatar
Scott Cyphers committed
1483
{
1484
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
Scott Cyphers's avatar
Scott Cyphers committed
1485 1486
    auto backend = manager->allocate_backend();

1487
    test::Uniform<float> rng(-1.0f, 1.0f);
1488
    Shape shape{2, 3};
1489 1490 1491
    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
1492 1493

    auto make_graph = [shape]() {
1494 1495 1496
        auto X0 = make_shared<op::Parameter>(element::f32, shape);
        auto X1 = make_shared<op::Parameter>(element::f32, shape);
        auto X2 = make_shared<op::Parameter>(element::f32, shape);
1497 1498
        return make_shared<Function>((X0 + X1) * X2,
                                     std::vector<std::shared_ptr<op::Parameter>>{X0, X1, X2});
Scott Cyphers's avatar
Scott Cyphers committed
1499
    };
1500 1501
    EXPECT_TRUE(
        autodiff_numeric_compare<float>(manager, backend, make_graph, {x0, x1, x2}, .01f, .01f));
Scott Cyphers's avatar
Scott Cyphers committed
1502
}
1503 1504 1505

TEST(${BACKEND_NAME}, backwards_reverse_3d_02)
{
fenglei.tian's avatar
fenglei.tian committed
1506
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1507 1508 1509 1510
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

    test::Uniform<float> rng(-1.0f, 1.0f);
1511
    Shape shape{2, 4, 5};
1512 1513 1514 1515 1516 1517 1518 1519 1520
    auto x = rng.initialize(backend->make_primary_tensor_view(element::f32, shape));

    auto make_graph = [shape]() {
        auto X = make_shared<op::Parameter>(element::f32, shape);
        return make_shared<Function>(make_shared<op::Reverse>(X, AxisSet{0, 2}),
                                     std::vector<std::shared_ptr<op::Parameter>>{X});
    };
    EXPECT_TRUE(autodiff_numeric_compare<float>(manager, backend, make_graph, {x}, .01f, .01f));
}
1521 1522 1523

TEST(${BACKEND_NAME}, backwards_maxpool_n4c1h4w4_kh2kw2_sh1sw1)
{
1524
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1525 1526
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();
1527
    Shape shape_a{4, 1, 4, 4}; //in NCHW
1528 1529 1530 1531 1532
    Shape maxpool_shape{4, 1, 3, 3};

    auto A = make_shared<op::Parameter>(element::f32, shape_a);
    Shape window_shape{2, 2};
    auto window_movement_strides = Strides{1, 1};
1533
    auto maxpool = make_shared<op::MaxPool>(A, window_shape, window_movement_strides);
1534
    auto f = make_shared<Function>(maxpool, op::ParameterVector{A});
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::f32, maxpool_shape);
    vector<float> dataEp(shape_size(maxpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::f32, shape_a);
    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::f32, shape_a);

    vector<float> dataInput{11, 65, 44, 28, 31, 33, 21, 66, 40, 49, 69, 57, 47, 30, 24, 27,
pthoreho's avatar
pthoreho committed
1545 1546 1547
                            13, 56, 46, 60, 61, 41, 25, 42, 48, 53, 51, 43, 59, 58, 29, 71,
                            17, 22, 72, 18, 39, 35, 15, 38, 64, 52, 73, 67, 62, 50, 10, 68,
                            45, 63, 16, 14, 55, 54, 37, 20, 36, 12, 70, 34, 19, 26, 32, 23};
1548 1549

    vector<float> expected{//delta
1550 1551 1552
                           0, 8, 0, 0, 0, 0, 0, 4, 0, 8, 16, 0, 0, 0, 0,  0, 0, 4, 0, 4, 8,  0,
                           0, 0, 0, 4, 4, 0, 4, 4, 0, 4, 0,  0, 8, 0, 4,  0, 0, 0, 8, 0, 16, 0,
                           0, 0, 0, 0, 0, 8, 0, 0, 4, 0, 4,  0, 4, 0, 16, 0, 0, 0, 0, 0};
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::f32, maxpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
    ASSERT_TRUE(read_vector<float>(output) == expected);
}

TEST(${BACKEND_NAME}, backwards_maxpool_n2c1h5w5_kh3kw3_sh2sw2)
{
1567
    SKIP_TEST_FOR("GPU", "${BACKEND_NAME}");
1568 1569 1570
    auto manager = runtime::Manager::get("${BACKEND_NAME}");
    auto backend = manager->allocate_backend();

1571
    Shape shape_a{1, 2, 5, 5}; //in NCHW
1572 1573 1574 1575 1576
    Shape maxpool_shape{1, 2, 2, 2};

    auto A = make_shared<op::Parameter>(element::f32, shape_a);
    Shape window_shape{3, 3};
    auto window_movement_strides = Strides{2, 2};
1577
    auto maxpool = make_shared<op::MaxPool>(A, window_shape, window_movement_strides);
1578
    auto f = make_shared<Function>(maxpool, op::ParameterVector{A});
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

    shared_ptr<runtime::TensorView> ep =
        backend->make_primary_tensor_view(element::f32, maxpool_shape);
    vector<float> dataEp(shape_size(maxpool_shape), 4);

    shared_ptr<runtime::TensorView> input =
        backend->make_primary_tensor_view(element::f32, shape_a);
    shared_ptr<runtime::TensorView> output =
        backend->make_primary_tensor_view(element::f32, shape_a);

    vector<float> dataInput{58, 15, 51, 35, 18, 47, 31, 32, 52, 21, 36, 38, 57, 54, 25, 45, 23,
pthoreho's avatar
pthoreho committed
1590 1591
                            30, 16, 27, 48, 20, 41, 37, 43, 39, 22, 28, 33, 29, 12, 17, 44, 42,
                            19, 40, 10, 46, 34, 53, 26, 55, 50, 13, 24, 14, 49, 56, 59, 11};
1592 1593

    vector<float> expected{//delta
1594 1595 1596
                           4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0,
                           0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 0, 0, 0,  4, 4, 0};
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

    copy_data(ep, dataEp);
    copy_data(input, dataInput);

    auto C = make_shared<op::Parameter>(element::f32, maxpool_shape);
    auto df = autodiff::backprop_function(f);
    auto external = manager->compile(df);
    auto cf = backend->make_call_frame(external);
    cf->tensor_call({input, ep}, {output});
    ASSERT_TRUE(read_vector<float>(output) == expected);
1607
}