Example #1
0
    def init_fc_param(self, n_out, load_from_bin=False):
        key = "fc_" + str(n_out)
        self.ge_param[key]["in_shape"][0] = self.ge_param["sample"]["batch_size"]
        w_shape = self.ge_param[key]["w_shape"]
        b_shape = self.ge_param[key]["b_shape"]

        if load_from_bin:
            if os.path.exists('./data/' + key + '_w_1.bin'):
                self.ge_param[key]["b"] = np.fromfile('./data/' + key + '_b_1.bin', dtype=np.float16)
                self.ge_param[key]["w"] = np.fromfile('./data/' + key + '_w_1.bin', dtype=np.float16)
            else:
                self.ge_param[key]["b"] = np.fromfile('./data/' + key + '_b_0.bin', dtype=np.float16)
                self.ge_param[key]["w"] = np.fromfile('./data/' + key + '_w_0.bin', dtype=np.float16)
        else:
            self.ge_param[key]["b"] = self.gen_tensor_data(b_shape, value=0.02, dt=self.ge_param[key]["dt"])
            self.ge_param[key]["w"] = self.gen_tensor_data_normal(w_shape, dt=self.ge_param[key]["dt"])
        self.ge_param[key]["w_acc"] = self.gen_tensor_data(w_shape, value=0.0001, dt=self.ge_param[key]["dt"])
        self.ge_param[key]["b_acc"] = self.gen_tensor_data(b_shape, value=0.0001, dt=self.ge_param[key]["dt"])
        print(key, self.ge_param[key]["w"])

        w_desc = ge.TensorDesc(ge.Shape(w_shape), self.ge_param[key]["fmt"], self.ge_param[key]["dt"])
        b_desc = ge.TensorDesc(ge.Shape(b_shape), self.ge_param[key]["fmt"], self.ge_param[key]["dt"])
        self.ge_param[key]["var_desc"] = [w_desc, b_desc, w_desc, b_desc]
        self.ge_param[key]["var_tensor"] = [
                self.gen_tensor(w_shape, self.ge_param[key]["w"], fmt=self.ge_param[key]["fmt"],
                                dt=self.ge_param[key]["dt"]),
                self.gen_tensor(b_shape, self.ge_param[key]["b"], fmt=self.ge_param[key]["fmt"],
                                dt=self.ge_param[key]["dt"]),
                self.gen_tensor(w_shape, self.ge_param[key]["w_acc"], fmt=self.ge_param[key]["fmt"],
                                dt=self.ge_param[key]["dt"]),
                self.gen_tensor(b_shape, self.ge_param[key]["b_acc"], fmt=self.ge_param[key]["fmt"],
                                dt=self.ge_param[key]["dt"])
            ]
Example #2
0
def test_random():
    """
    op random
    """
    config = {"ge.exec.deviceId": "0", "ge.graphRunMode": "1"}
    options = {}
    ge_handle = PyGe(config, options)

    graph = ge.Graph("RandomUniform")

    # input
    size = 2
    np_data = np.array([2, 2]).astype(np.int32)
    np_data = np.frombuffer(np_data.tobytes(), dtype=np.uint8)
    input_tensor_desc = ge.TensorDesc(ge.Shape([2]), ge.FORMAT_NHWC,
                                      ge.DT_INT32)
    input_tensor = ge.Tensor(input_tensor_desc, np_data)
    input_random = []
    input_random.append(input_tensor)

    # build graph
    np_in = np.array([2, 2, 2, 2]).astype(np.int32)
    data_buffer = np.frombuffer(np_in.tobytes(), dtype=np.uint8)
    shape_tensor_desc = ge.TensorDesc(ge.Shape([4]), ge.FORMAT_NHWC,
                                      ge.DT_INT32)
    shape_tensor_desc.set_real_dim_cnt(1)
    shape_tensor = ge.Tensor(shape_tensor_desc, data_buffer)
    data1 = ge.OperatorFactory.create_operator("const",
                                               "Const").set_attr_tensor(
                                                   "value", shape_tensor)
    shape_const_desc = ge.TensorDesc(ge.Shape([4]), ge.FORMAT_NHWC,
                                     ge.DT_INT32)
    data1.update_output_desc("y", shape_const_desc)
    result_output = ge.OperatorFactory.create_operator(
        "random",
        "RandomUniform").set_input("shape",
                                   data1).set_attr_dtype("dtype", ge.DT_FLOAT)

    inputs = [data1]
    outputs = [result_output]

    graph.set_inputs(inputs).set_outputs(outputs)

    ge_handle.add_graph(0, graph)
    output_random = ge_handle.run_graph(0, input_random)

    print('out = ', ge_handle.get_tensor_data(output_random[0], np.float32))

    ge_handle.session.remove_graph(0)
Example #3
0
 def init_sample_param(self, h, w, channel, batch_size=1):
     self.ge_param["sample"]["batch_size"] = batch_size
     self.ge_param["sample"]["h"] = h
     self.ge_param["sample"]["w"] = w
     self.ge_param["sample"]["channel"] = channel
     self.ge_param["sample"]["tensor"] = self.gen_tensor([1], self.ge_param["sample"]["lr"],
                                                         ge.FORMAT_NHWC, ge.DT_FLOAT16)
     self.ge_param["sample"]["desc"] = ge.TensorDesc(ge.Shape([1]), ge.FORMAT_NHWC, ge.DT_FLOAT16)
Example #4
0
    def init_conv2d_param(self, filters, load_from_bin=False):
        key = "conv2D_" + str(filters)
        self.ge_param[key]["in_shape"][0] = self.ge_param["sample"]["batch_size"]
        w_shape = self.ge_param[key]["w_shape"]
        in_shape = self.ge_param[key]["in_shape"]
        b_shape = self.ge_param[key]["b_shape"]
        self.ge_param[key]["w_shape_tensor"] = self.gen_tensor([len(self.ge_param[key]['w_shape']), ],
                                               self.ge_param[key]['w_shape'], 
                                               fmt=self.ge_param[key]["fmt"], 
                                               dt=ge.DT_INT32)
        if load_from_bin:
            if os.path.exists('./data/' + key + '_w_1.bin'):
                self.ge_param[key]["b"] = np.fromfile('./data/' + key + '_b_1.bin', dtype=np.float16)
                self.ge_param[key]["w"] = np.fromfile('./data/' + key + '_w_1.bin', dtype=np.float16)
            else:
                self.ge_param[key]["b"] = np.fromfile('./data/' + key + '_b_0.bin', dtype=np.float16)
                self.ge_param[key]["w"] = np.fromfile('./data/' + key + '_w_0.bin', dtype=np.float16)
        else:
            self.ge_param[key]["w"] = self.gen_tensor_data_normal(w_shape, dt=self.ge_param[key]["dt"])
            self.ge_param[key]["b"] = self.gen_tensor_data(b_shape, value=0.03, dt=self.ge_param[key]["dt"])
        self.ge_param[key]["w_acc"] = self.gen_tensor_data(w_shape, value=0.0001, dt=self.ge_param[key]["dt"])
        self.ge_param[key]["b_acc"] = self.gen_tensor_data(b_shape, value=0.0001, dt=self.ge_param[key]["dt"])
        print(key, self.ge_param[key]["w"])

        self.ge_param[key]["strides"] = (1, 1, 1, 1)
        self.ge_param[key]["pads"] = (2, 2, 2, 2)
        self.ge_param[key]["in_shape_tensor"] = self.gen_tensor([4], in_shape, fmt=self.ge_param[key]["fmt"],
                                                         dt=ge.DT_INT32)
        tensor_desc = ge.TensorDesc(ge.Shape(w_shape), self.ge_param[key]["fmt"], self.ge_param[key]["dt"])
        b_desc = ge.TensorDesc(ge.Shape(b_shape), self.ge_param[key]["fmt"], self.ge_param[key]["dt"])
        self.ge_param[key]["var_desc"] = [tensor_desc, b_desc, tensor_desc, b_desc]
        self.ge_param[key]["var_tensor"] = [
            self.gen_tensor(w_shape, self.ge_param[key]["w"], fmt=self.ge_param[key]["fmt"],
                            dt=self.ge_param[key]["dt"]),
            self.gen_tensor(b_shape, self.ge_param[key]["b"], fmt=self.ge_param[key]["fmt"],
                            dt=self.ge_param[key]["dt"]),
            self.gen_tensor(w_shape, self.ge_param[key]["w_acc"], fmt=self.ge_param[key]["fmt"],
                            dt=self.ge_param[key]["dt"]),
            self.gen_tensor(b_shape, self.ge_param[key]["b_acc"], fmt=self.ge_param[key]["fmt"],
                            dt=self.ge_param[key]["dt"])
        ]
Example #5
0
    def get_tensor_from_bin(self, in_path, shape_list, fmt=ge.FORMAT_ND, dt=ge.DT_FLOAT16):
        size = 1
        for i in range(len(shape_list)):
            size *= shape_list[i]
        data_len = size * self.get_data_type_size(dt)
        np_in = np.fromfile(in_path, dtype=np.uint8)
        np_size = np_in.size * np_in.itemsize

        check_equal("get_tensor_from_bin:", np_size, data_len)
        input_tensor_desc = ge.TensorDesc(ge.Shape(shape_list), fmt, dt)
        input_tensor_desc.set_real_dim_cnt(len(shape_list))
        input_tensor = ge.Tensor(input_tensor_desc, np_in)
        return input_tensor
Example #6
0
    def gen_tensor(self, tensor_shape, tensor_data, fmt=ge.FORMAT_NCHW, dt=ge.DT_FLOAT16):
        if dt == ge.DT_FLOAT:
            data_type = np.float32
        elif dt == ge.DT_FLOAT16:
            data_type = np.float16
        elif dt == ge.DT_INT32:
            data_type = np.int32
        else:
            data_type = np.int8
        
        np_data = np.array(tensor_data, dtype=data_type)
        np_data = np.frombuffer(np_data.tobytes(), dtype=np.uint8)
        input_tensor_desc = ge.TensorDesc(ge.Shape(tensor_shape), fmt, dt)
        tensor = ge.Tensor(input_tensor_desc, np_data)

        return tensor
Example #7
0
def gen_tensor(tensor_shape, value):
    size = 1
    for i in range(len(tensor_shape)):
        size *= tensor_shape[i]

    np_data = np.zeros(size, dtype=np.float16)
    for i in range(size):
        np_data[i] = value

    input_tensor_desc = ge.TensorDesc(ge.Shape(tensor_shape), ge.FORMAT_ND,
                                      ge.DT_FLOAT16)
    tensor = ge.Tensor()
    tensor.set_tensor_desc(input_tensor_desc)
    tensor.set_data(np_data)

    return tensor
Example #8
0
def get_tensor_from_bin(in_path,
                        shape_list,
                        format=ge.FORMAT_ND,
                        data_type=ge.DT_FLOAT16):
    size = 1
    for i in range(len(shape_list)):
        size *= shape_list[i]
    data_len = size * get_data_type_size(data_type)

    np_in = np.fromfile(in_path, dtype=np.uint8)
    np_size = np_in.size * np_in.itemsize
    assert np_size == data_len

    input_tensor_desc = ge.TensorDesc(ge.Shape(shape_list), format, data_type)
    input_tensor_desc.set_real_dim_cnt(len(shape_list))
    input_tensor = ge.Tensor(input_tensor_desc, np_in)

    return input_tensor
Example #9
0
    def gen_tensor(self, tensor_shape, value, data_type):
        """
        generate tensor
        """
        size = 1
        for i in range(len(tensor_shape)):
            size *= tensor_shape[i]

        np_data = np.zeros(size, dtype=np.float16)
        for i in range(size):
            np_data[i] = value
        np_data = np.frombuffer(np_data.tobytes(), dtype=np.uint8)

        input_tensor_desc = ge.TensorDesc(ge.Shape(tensor_shape), ge.FORMAT_ND,
                                          data_type)
        tensor = ge.Tensor(input_tensor_desc, np_data)

        return tensor
Example #10
0
 def reshape(self, tensor, shape):
     tensor_data = np.array(tensor.get_data(), dtype=np.uint8)
     tensor_desc = ge.TensorDesc(ge.Shape(shape),
                                 tensor.get_tensor_desc().get_format(),
                                 tensor.get_tensor_desc().get_data_type())
     tensor.set_tensor_desc(tensor_desc)
Example #11
0
def test_graph():
    # 0. System init
    config = {
        "ge.exec.deviceId": "0",
        "ge.graphRunMode": "1"
    }
    ret = ge.ge_initialize(config)
    check_ret("ge_initialize", ret)
    print("Initialize ge success.")

    # 1. Generate graph
    desc = ge.TensorDesc(ge.Shape([2,1]), ge.FORMAT_ND, ge.DT_FLOAT16)
    var_tensor_desc = [desc, desc]

    # 1.1 init graph
    init_graph_id = 0
    init_var_graph = graph_utils.gen_init_graph("InitVarGraph", var_tensor_desc, var_name, [0, 0])
    print("Generate init graph success.")

    # 1.2 add graph
    add_graph_id = 1
    add_graph = graph_utils.gen_add_graph("AddGraph", var_tensor_desc, var_name)
    print("Generate add graph success.")

    # 2. create session
    options = {
        "a": "b",
        "ge.trainFlag": "1"
    }
    session = ge.Session(options)
    if session:
        print("Create session success.")
    else:
        print("Create session fail.")

    # 3. add graph
    ret = session.add_graph(init_graph_id, init_var_graph)
    check_ret("AddGraph init_graph_id", ret)
    print("Session add init graph success.")
    ret = session.add_graph(add_graph_id, add_graph)
    check_ret("AddGraph add_graph_id", ret)
    print("Session add ADD graph success.")

    # 4. Run graph
    input_init, input_add = [], []
    output_init, output_add = [], []
    output_init, ret = session.run_graph(init_graph_id, input_init)
    check_ret("RunGraph init_graph_id", ret)
    print("Session run Init graph success.")

    input_shapes = [2, 1]
    input_a_path = PATH + "ge_variable_input_a.bin"
    input_b_path = PATH + "ge_variable_input_b.bin"
    input_tensor_a = graph_utils.get_tensor_from_bin(input_a_path, input_shapes)
    input_add.append(input_tensor_a)
    input_tensor_b = graph_utils.get_tensor_from_bin(input_b_path, input_shapes)
    input_add.append(input_tensor_b)

    output_add, ret = session.run_graph(add_graph_id, input_add)
    check_ret("RunGraph add_graph_id", ret)
    print("Session run add graph success.")

    print('a=', graph_utils.get_tensor_data(input_add[0], np.float16),
        '\nb=', graph_utils.get_tensor_data(input_add[1], np.float16),
        '\nout=', graph_utils.get_tensor_data(output_add[0], np.float16))

    # 5.Optional operation: If a graph is runned before, and want to run again,
    # you need to check whether graph needed to rebuild,
    # so a graph is runned before, and needed to rebuild, user should remove it from GE first,
    # then add graph again and rebuild it.
    # 6. system finalize
    ret = ge.ge_finalize()
    check_ret("ge_finalize", ret)
    print("Finalize ge success.")