op.cpp 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/**
 * \file src/custom/test/op.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "megbrain/custom/op.h"
#include "megbrain/comp_node.h"
#include "megbrain/tensor.h"
#include "megbrain/custom/data_adaptor.h"
#include "gtest/gtest.h"
#include "megbrain_build_config.h"

#define OP_TEST_LOG 0

using namespace mgb;

namespace custom {

TEST(TestCustomOp, TestCustomOpInfoSetter) {
    CustomOp test("TestOp", CUSTOM_OP_VERSION);
    test.set_description("Test Op")
        .add_input("lhs", "lhs of test op", {"float32", "int32"}, 2)
        .add_inputs(2)
        .add_input("rhs", "rhs of test op", {"float32", "int32"}, 2)
        .add_outputs(1)
        .add_output("out", "out of test op", {"float32", "int32"}, 2)
        .add_outputs(3);

    ASSERT_TRUE(test.op_type() == "TestOp");
    ASSERT_TRUE(test.op_desc() == "Test Op");
    ASSERT_TRUE(test.input_num() == 4);
    ASSERT_TRUE(test.output_num() == 5);

#if OP_TEST_LOG
    for (auto input: test.inputs_info()) {
        std::cout << input.str() << std::endl;
    }
    for (auto output: test.outputs_info()) {
        std::cout << output.str() << std::endl;
    }
#endif

    test.add_param("param1", "param1 - float", 1.23f)
        .add_param("param2", "param2 - float list", {2.34f, 3.45f})
        .add_param("param3", "param3 - string", "test-string")
        .add_param("param4", {"test", "string", "list"})
        .add_param("param5", 1);

#if OP_TEST_LOG
    ParamInfo pinfo = test.param_info();
    for (auto kv: pinfo.meta()) {
        std::cout << kv.str() << std::endl;
    }
#endif
}

void device_infer(const std::vector<Device> &inputs, const Param &params,
                  std::vector<Device> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
    outputs[0] = inputs[1];
    outputs[1] = inputs[0];
}

void shape_infer(const std::vector<Shape> &inputs, const Param &params,
                 std::vector<Shape> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
    outputs[0] = inputs[1];
    outputs[1] = inputs[0];
}

void dtype_infer(const std::vector<DType> &inputs, const Param &params,
                 std::vector<DType> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
    outputs[0] = inputs[1];
    outputs[1] = inputs[0];
}

void format_infer(const std::vector<Format> &inputs, const Param &params,
                  std::vector<Format> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
    outputs[0] = inputs[1];
    outputs[1] = inputs[0];
}

void cpu_kernel(const std::vector<Tensor> &inputs, const Param &params,
             std::vector<Tensor> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
#if OP_TEST_LOG
    std::cout << "Checking CPU Forward - " << params["device"].as<std::string>() << std::endl;
#endif
    ASSERT_TRUE(params["device"] == "x86");
}

void gpu_kernel(const std::vector<Tensor> &inputs, const Param &params,
             std::vector<Tensor> &outputs) {
    (void)inputs;
    (void)params;
    (void)outputs;
#if OP_TEST_LOG
    std::cout << "Checking GPU Forward - " << params["device"].as<std::string>() << std::endl;
#endif
    ASSERT_TRUE(params["device"] == "cuda");
}

TEST(TestCustomOp, TestCustomOpFuncSetter) {
#if MGB_CUDA
    CustomOp test("TestOp", CUSTOM_OP_VERSION);
    test.set_description("Test Op Forward Backward Union")
        .add_input("lhs", "lhs of Test op", {"float32", "int32"}, 2)
        .add_input("rhs", "rhs of Test op", {"float32", "int32"}, 2)
        .add_output("outl", "outl of Test op", {"float32", "int32"}, 2)
        .add_output("outr", "outr of Test op", {"float32", "int32"}, 2)
        .add_param("smooth", "smooth", 0.f)
        .add_param("device", "using for judge device", "x86");

    std::vector<Device> idevices = {"x86", "cuda"};
    std::vector<Shape>  ishapes  = {{2, 3}, {3, 4}};
    std::vector<DType>  idtypes  = {"int32", "float32"};
    std::vector<Format> iformats = {"default", "default"};
    Param param(test.param_info());

    std::vector<Device> odevices = test.infer_output_device(idevices, param);
    std::vector<Shape>  oshapes  = test.infer_output_shape (ishapes,  param);
    std::vector<DType>  odtypes  = test.infer_output_dtype (idtypes,  param);
    std::vector<Format> oformats = test.infer_output_format(iformats, param);

    ASSERT_TRUE(odevices.size() == 2);
    ASSERT_TRUE(oshapes.size()  == 2);
    ASSERT_TRUE(odtypes.size()  == 2);
    ASSERT_TRUE(oformats.size() == 2);

    ASSERT_TRUE(odevices[0] == "x86");
    ASSERT_TRUE(odevices[1] == "x86");
    ASSERT_TRUE(oshapes[0]  == Shape({2,3}));
    ASSERT_TRUE(oshapes[1]  == Shape({2,3}));
    ASSERT_TRUE(odtypes[0]  == "int32");
    ASSERT_TRUE(odtypes[1]  == "int32");
    ASSERT_TRUE(iformats[0].is_default());
    ASSERT_TRUE(iformats[1].is_default());

    test.set_device_infer(device_infer)
        .set_shape_infer(shape_infer)
        .set_dtype_infer(dtype_infer)
        .set_format_infer(format_infer);

    odevices = test.infer_output_device(idevices, param);
    oshapes  = test.infer_output_shape (ishapes,  param);
    odtypes  = test.infer_output_dtype (idtypes,  param);
    oformats = test.infer_output_format(iformats, param);

    ASSERT_TRUE(odevices.size() == 2);
    ASSERT_TRUE(oshapes.size()  == 2);
    ASSERT_TRUE(odtypes.size()  == 2);
    ASSERT_TRUE(oformats.size() == 2);

    ASSERT_TRUE(odevices[0] == "cuda");
    ASSERT_TRUE(odevices[1] == "x86");
    ASSERT_TRUE(oshapes[0]  == Shape({3,4}));
    ASSERT_TRUE(oshapes[1]  == Shape({2,3}));
    ASSERT_TRUE(odtypes[0]  == "float32");
    ASSERT_TRUE(odtypes[1]  == "int32");
    ASSERT_TRUE(iformats[0].is_default());
    ASSERT_TRUE(iformats[1].is_default());

    test.set_compute(cpu_kernel);
    DeviceTensorND cdev_itensor0(CompNode::load("cpux"), {3, 2}, dtype::Int32{});
    DeviceTensorND cdev_itensor1(CompNode::load("cpux"), {3, 2}, dtype::Float32{});
    DeviceTensorND cdev_otensor0(CompNode::load("cpux"), {3, 2}, dtype::Float32{});
    DeviceTensorND cdev_otensor1(CompNode::load("cpux"), {3, 2}, dtype::Int32{});

    std::vector<Tensor> cinputs = {to_custom_tensor(cdev_itensor0), to_custom_tensor(cdev_itensor1)};
    std::vector<Tensor> coutputs ={to_custom_tensor(cdev_otensor0), to_custom_tensor(cdev_otensor1)};
    param["device"] = "x86";
    test.compute(cinputs, param, coutputs);

    test.set_compute("cuda", gpu_kernel);
    DeviceTensorND gdev_itensor0(CompNode::load("gpux"), {3, 2}, dtype::Int32{});
    DeviceTensorND gdev_itensor1(CompNode::load("gpux"), {3, 2}, dtype::Float32{});
    DeviceTensorND gdev_otensor0(CompNode::load("gpux"), {3, 2}, dtype::Float32{});
    DeviceTensorND gdev_otensor1(CompNode::load("gpux"), {3, 2}, dtype::Int32{});

    std::vector<Tensor> ginputs = {to_custom_tensor(gdev_itensor0), to_custom_tensor(gdev_itensor1)};
    std::vector<Tensor> goutputs ={to_custom_tensor(gdev_otensor0), to_custom_tensor(gdev_otensor1)};
    param["device"] = "cuda";
    test.compute(ginputs, param, goutputs);
#endif
}

}