|
| 1 | +#include "tensorflow/core/common_runtime/kernel_benchmark_testlib.h" |
| 2 | +#include "tensorflow/core/framework/fake_input.h" |
| 3 | +#include "tensorflow/core/framework/node_def_builder.h" |
| 4 | +#include "tensorflow/core/framework/tensor.h" |
| 5 | +#include "tensorflow/core/framework/types.h" |
| 6 | +#include "tensorflow/core/kernels/conv_ops_gpu.h" |
| 7 | +#include "tensorflow/core/kernels/ops_testutil.h" |
| 8 | +#include "tensorflow/core/kernels/ops_util.h" |
| 9 | +#include "tensorflow/core/platform/test.h" |
| 10 | +#include "tensorflow/core/platform/test_benchmark.h" |
| 11 | +#include "tensorflow/core/public/session.h" |
| 12 | +#include "tensorflow/cc/ops/standard_ops.h" |
| 13 | + |
| 14 | +namespace tensorflow { |
| 15 | +namespace { |
| 16 | + |
| 17 | +enum class Device { CPU, GPU }; |
| 18 | + |
| 19 | +class FusedL2NormalizeGradOpTest : public OpsTestBase { |
| 20 | + protected: |
| 21 | + void MakeOpAndSetDevice(Device device, DataType dtype, int axis, float epsilon) { |
| 22 | + TF_EXPECT_OK(NodeDefBuilder("fused_l2_normalize_grad", "FusedL2NormalizeGrad") |
| 23 | + .Attr("T", dtype) |
| 24 | + .Attr("T", dtype) |
| 25 | + .Attr("axis", axis) |
| 26 | + .Attr("epsilon", epsilon) |
| 27 | + .Input(FakeInput(DT_FLOAT)) |
| 28 | + .Input(FakeInput(DT_FLOAT)) |
| 29 | + .Finalize(node_def())); |
| 30 | + TF_EXPECT_OK(InitOp()); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +TEST_F(FusedL2NormalizeGradOpTest, 2Dims_Float) { |
| 35 | + const int rows = 4; |
| 36 | + const int cols = 252; //128+64+32+16+8+4=252 1008 |
| 37 | + |
| 38 | + MakeOpAndSetDevice(Device::CPU, DT_FLOAT, 0, 1e-12); |
| 39 | + |
| 40 | + // y_grad |
| 41 | + float y_grad_array[1008]; |
| 42 | + for (int i = 0; i < rows * cols; i++) { |
| 43 | + y_grad_array[i] = 1.0; |
| 44 | + } |
| 45 | + y_grad_array[251] = 2.0; |
| 46 | + y_grad_array[503] = 2.0; |
| 47 | + y_grad_array[755] = 2.0; |
| 48 | + y_grad_array[1007] = 2.0; |
| 49 | + AddInputFromArray<float>(TensorShape({rows, cols}), y_grad_array); |
| 50 | + |
| 51 | + // x |
| 52 | + float x_array[1008]; |
| 53 | + for (int i = 0; i < rows * cols; i++) { |
| 54 | + x_array[i] = 1.0; |
| 55 | + } |
| 56 | + AddInputFromArray<float>(TensorShape({rows, cols}), x_array); |
| 57 | + |
| 58 | + TF_ASSERT_OK(RunOpKernel()); |
| 59 | + TF_EXPECT_OK(device_->Sync()); |
| 60 | + |
| 61 | + { |
| 62 | + Tensor expected_output(allocator(), DT_FLOAT, |
| 63 | + TensorShape({rows, cols})); |
| 64 | + float output_array[1008]; |
| 65 | + for (int i = 0; i < rows * cols; i++) { |
| 66 | + output_array[i] = - 1.0 / (252 * std::sqrt(252)); |
| 67 | + } |
| 68 | + output_array[251] = 251.0 / (252 * std::sqrt(252)); |
| 69 | + output_array[503] = 251.0 / (252 * std::sqrt(252)); |
| 70 | + output_array[755] = 251.0 / (252 * std::sqrt(252)); |
| 71 | + output_array[1007] = 251.0 / (252 * std::sqrt(252)); |
| 72 | + test::FillValues<float>(&expected_output, output_array); |
| 73 | + test::ExpectTensorNear<float>(expected_output, *GetOutput(0), 1e-6); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +//----------------------------------------------------------------------------// |
| 78 | +// Performance benchmarks // |
| 79 | +//----------------------------------------------------------------------------// |
| 80 | +static Graph* FusedL2NormalizeGrad(int rows, int cols) { |
| 81 | + Graph* g = new Graph(OpRegistry::Global()); |
| 82 | + DataType dtype = DT_FLOAT; |
| 83 | + |
| 84 | + Tensor in1(dtype, TensorShape({rows, cols})); |
| 85 | + in1.flat<float>().setRandom(); |
| 86 | + Tensor in2(dtype, TensorShape({rows, cols})); |
| 87 | + in2.flat<float>().setRandom(); |
| 88 | + |
| 89 | + Node* input_in1 = test::graph::Constant(g, in1); |
| 90 | + Node* input_in2 = test::graph::Constant(g, in2); |
| 91 | + auto nodeBuilder = NodeBuilder(g->NewName("n"), "FusedL2NormalizeGrad") |
| 92 | + .Input(input_in1) |
| 93 | + .Input(input_in2) |
| 94 | + .Attr("T", dtype) |
| 95 | + .Attr("axis", 0) |
| 96 | + .Attr("epsilon", 1e-12); |
| 97 | + TF_CHECK_OK(nodeBuilder.Finalize(g, nullptr)); |
| 98 | + |
| 99 | + return g; |
| 100 | +} |
| 101 | + |
| 102 | +#define BM_FusedL2NormGrad(ROWS, COLS, NTH) \ |
| 103 | + static void BM_FusedL2NormGrad##_##ROWS##_##COLS##_##NTH##_CPU( \ |
| 104 | + int iters) { \ |
| 105 | + testing::UseRealTime(); \ |
| 106 | + testing::ItemsProcessed(static_cast<int64>(iters) * ROWS * COLS * 5); \ |
| 107 | + SessionOptions opts; \ |
| 108 | + opts.config.set_intra_op_parallelism_threads(NTH); \ |
| 109 | + test::Benchmark("cpu", FusedL2NormalizeGrad(ROWS, COLS), &opts).Run(iters); \ |
| 110 | + } \ |
| 111 | + BENCHMARK(BM_FusedL2NormGrad##_##ROWS##_##COLS##_##NTH##_CPU); \ |
| 112 | + |
| 113 | +#define BM_FusedL2NormGrad_NTH(ROWS, COLS) \ |
| 114 | + BM_FusedL2NormGrad(ROWS, COLS, 1); \ |
| 115 | + BM_FusedL2NormGrad(ROWS, COLS, 4); \ |
| 116 | + BM_FusedL2NormGrad(ROWS, COLS, 8); \ |
| 117 | + |
| 118 | +BM_FusedL2NormGrad_NTH(1024, 63); |
| 119 | +BM_FusedL2NormGrad_NTH(1024, 127); |
| 120 | +BM_FusedL2NormGrad_NTH(1024, 255); |
| 121 | +BM_FusedL2NormGrad_NTH(1024, 511); |
| 122 | +BM_FusedL2NormGrad_NTH(1024, 1023); |
| 123 | + |
| 124 | +} |
| 125 | +} |
0 commit comments