Exemplo n.º 1
0
    def _gen_input_dataset(self, input_list):
        ret = const.SUCCESS
        if len(input_list) != self._input_num:
            log_error("Current input data num %d unequal to model "
                      "input num %d" % (len(input_list), self._input_num))
            return const.FAILED

        self._input_dataset = acl.mdl.create_dataset()
        for i in range(self._input_num):
            item = input_list[i]
            data, size = self._parse_input_data(item, i)
            if (data is None) or (size == 0):
                ret = const.FAILED
                log_error("The %d input is invalid" % (i))
                break

            model_size = acl.mdl.get_input_size_by_index(self._model_desc, i)
            if size != model_size:
                log_warning(" Input[%d] size: %d not equal om size: %d" % (i, size, model_size) +\
                        ", may cause inference result error, please check model input")


            dataset_buffer = acl.create_data_buffer(data, size)
            _, ret = acl.mdl.add_dataset_buffer(self._input_dataset,
                                                dataset_buffer)
            if ret:
                log_error("Add input dataset buffer failed")
                acl.destroy_data_buffer(self._input_dataset)
                ret = const.FAILED
                break
        if ret == const.FAILED:
            self._release_dataset(self._input_dataset)
            self._input_dataset = None

        return ret
Exemplo n.º 2
0
    def _gen_input_dataset(self, input_list):
        ret = const.SUCCESS
        if len(input_list) != self._input_num:
            log_error("Current input data num %d unequal to model "
                      "input num %d" % (len(input_list), self._input_num))
            return const.FAILED

        self._input_dataset = acl.mdl.create_dataset()
        for i in range(self._input_num):
            item = input_list[i]
            data, size = self._parse_input_data(item, i)            
            if (data is None) or (size == 0):
                ret = const.FAILED
                log_error("The %d input is invalid" % (i))
                break
            dataset_buffer = acl.create_data_buffer(data, size)
            _, ret = acl.mdl.add_dataset_buffer(self._input_dataset,
                                                dataset_buffer)
            if ret:
                log_error("Add input dataset buffer failed")
                acl.destroy_data_buffer(self._input_dataset)
                ret = const.FAILED
                break
        if ret == const.FAILED:
            self._release_dataset(self._input_dataset)
            self._input_dataset = None

        return ret
Exemplo n.º 3
0
    def _gen_input_dataset(self, input_list):
        # organize input dataset structure 
        ret = SUCCESS
        # return if the input number does not match model requirements
        if len(input_list) != self._input_num:
            print("Current input data num %d unequal to"
                  " model input num %d"%(len(input_list), self._input_num))
            return FAILED

        self.input_dataset = acl.mdl.create_dataset()
        for i in range(self._input_num):
            item = input_list[i]
            # parse input, currently supports AclImage type, Acl pointer and numpy array
            data, size = self._parse_input_data(item, i)            
            if (data is None) or (size == 0):
                # not parse the remaining data when parsing data fails
                ret = FAILED
                print("The %d input is invalid"%(i))
                break
            # create input dataset buffer structure, fill in input data
            dataset_buffer = acl.create_data_buffer(data, size)
            # add dataset buffer to dataset 
            _, ret = acl.mdl.add_dataset_buffer(self.input_dataset,
                                                dataset_buffer)
            if ret:
                print("Add input dataset buffer failed")
                acl.destroy_data_buffer(self.input_dataset)
                ret = FAILED
                break
        if ret == FAILED:
            # release dataset if fails 
            self._release_dataset(self.input_dataset)

        return ret
Exemplo n.º 4
0
    def _gen_input_dataset(self, input_list):
        #组织输入数据的dataset结构
        ret = SUCCESS
        #如果输入的数据个数与模型要求的不匹配,则直接返回
        if len(input_list) != self._input_num:
            print("Current input data num %d unequal to"
                  " model input num %d" % (len(input_list), self._input_num))
            return FAILED

        self.input_dataset = acl.mdl.create_dataset()
        for i in range(self._input_num):
            item = input_list[i]
            #解析输入,当前支持输入AclImage类型、Acl指针和numpy数组
            data, size = self._parse_input_data(item, i)
            if (data is None) or (size == 0):
                #解析数据失败时不再解析剩余数据
                ret = FAILED
                print("The %d input is invalid" % (i))
                break
            #创建输入dataset buffer结构,填入输入的数据
            dataset_buffer = acl.create_data_buffer(data, size)
            #将dataset buffer加入dataset
            _, ret = acl.mdl.add_dataset_buffer(self.input_dataset,
                                                dataset_buffer)
            if ret:
                print("Add input dataset buffer failed")
                acl.destroy_data_buffer(self.input_dataset)
                ret = FAILED
                break
        if ret == FAILED:
            #失败时释放dataset
            self._release_dataset(self.input_dataset)

        return ret
Exemplo n.º 5
0
    def __del__(self):
        if self._input_desc:
            acl.destroy_tensor_desc(self._input_desc)

        if self._output_desc:
            acl.destroy_tensor_desc(self._output_desc)

        if self.dev_buffer_cast:
            ret = acl.rt.free(self.dev_buffer_cast)
            check_ret("acl.rt.free", ret)

        if self.input_desc_arg_max_d:
            acl.destroy_tensor_desc(self.input_desc_arg_max_d)

        if self.output_desc_arg_max_d:
            acl.destroy_tensor_desc(self.output_desc_arg_max_d)

        if self.op_attr:
            acl.op.destroy_attr(self.op_attr)

        if self.dev_buffer_arg_max_d:
            ret = acl.rt.free(self.dev_buffer_arg_max_d)
            check_ret("acl.rt.free", ret)

        if self.output_buffer_cast:
            ret = acl.destroy_data_buffer(self.output_buffer_cast)
            check_ret("acl.destroy_data_buffer", ret)

        if self.output_buffer_arg_max_d:
            ret = acl.destroy_data_buffer(self.output_buffer_arg_max_d)
            check_ret("acl.destroy_data_buffer", ret)

        print("[SingOp] class SingOp release source success")
Exemplo n.º 6
0
    def release_resource(self):
        print('release source stage:')
        while self._inputs_desc:
            ret = acl.destroy_data_buffer(self._inputs_device_buffer.pop())
            check_ret("acl.destroy_data_buffer", ret)
            ret = acl.rt.free(self._inputs_device.pop())
            check_ret("acl.rt.free", ret)
            acl.destroy_tensor_desc(self._inputs_desc.pop())

        while self.output_desc:
            ret = acl.destroy_data_buffer(self.device_buffer_outputs.pop())
            check_ret("acl.destroy_data_buffer", ret)
            ret = acl.rt.free(self.device_outputs.pop())
            check_ret("acl.rt.free", ret)
            acl.destroy_tensor_desc(self.output_desc.pop())

        if self.op_attr:
            acl.op.destroy_attr(self.op_attr)
            self.op_attr = None

        if self.stream:
            ret = acl.rt.destroy_stream(self.stream)
            check_ret("acl.rt.destroy_stream", ret)
            self.stream = None

        if self.context:
            ret = acl.rt.destroy_context(self.context)
            check_ret("acl.rt.destroy_context", ret)
            self.context = None

        ret = acl.rt.reset_device(self.device_id)
        check_ret("acl.rt.reset_device", ret)
        ret = acl.finalize()
        check_ret("acl.finalize", ret)
        print('release source success')
    def _gen_dataset(self, type_str="input"):
        """According buffer to create dataset(acl.mdl.create_dataset()) object for execute 

        Args:
            type_str(str): For choose to create  which dataset(input or output)
        """
        dataset = acl.mdl.create_dataset()

        temp_dataset = None
        if type_str == "in":
            self.load_input_dataset = dataset
            temp_dataset = self.input_data
        else:
            self.load_output_dataset = dataset
            temp_dataset = self.output_data

        for item in temp_dataset:
            data = acl.create_data_buffer(item["buffer"], item["size"])
            if data is None:
                ret = acl.destroy_data_buffer(dataset)
                check_ret("acl.destroy_data_buffer", ret)

            _, ret = acl.mdl.add_dataset_buffer(dataset, data)

            if ret != ACL_ERROR_NONE:
                ret = acl.destroy_data_buffer(dataset)
                check_ret("acl.destroy_data_buffer", ret)
Exemplo n.º 8
0
 def _gen_output_dataset(self, size):
     print("[Model] create model output dataset:")
     dataset = acl.mdl.create_dataset()
     for i in range(size):
         size = acl.mdl.get_output_size_by_index(self.model_desc, i)
         buffer, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY)
         check_ret("acl.rt.malloc", ret)
         dataset_buffer = acl.create_data_buffer(buffer, size)
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         if ret:
             acl.rt.free(buffer)
             acl.destroy_data_buffer(dataset)
             check_ret("acl.destroy_data_buffer", ret)
     self.output_dataset = dataset
     print("[Model] create model output dataset success")
Exemplo n.º 9
0
    def _load_input_data(self, images_data):
        if "bytes_to_ptr" in dir(acl.util):
            bytes_data = images_data.tobytes()
            img_ptr = acl.util.bytes_to_ptr(bytes_data)
        else:
            img_ptr = acl.util.numpy_to_ptr(images_data)  # host ptr
        # memcopy host to device
        image_buffer_size = images_data.size * images_data.itemsize
        img_device, ret = acl.rt.malloc(
            image_buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY)
        check_ret("acl.rt.malloc", ret)
        ret = acl.rt.memcpy(img_device, image_buffer_size, img_ptr,
                            image_buffer_size, ACL_MEMCPY_HOST_TO_DEVICE)
        check_ret("acl.rt.memcpy", ret)

        # create dataset in device
        img_dataset = acl.mdl.create_dataset()
        img_data_buffer = acl.create_data_buffer(img_device, image_buffer_size)
        if img_data_buffer is None:
            raise Exception("can't create data buffer, create input failed!!!")

        _, ret = acl.mdl.add_dataset_buffer(img_dataset, img_data_buffer)
        if ret != ACL_SUCCESS:
            ret = acl.destroy_data_buffer(img_data_buffer)
            check_ret("acl.destroy_data_buffer", ret)
        return img_dataset
Exemplo n.º 10
0
 def _gen_input_dataset(self, data, data_size):
     self.input_dataset = acl.mdl.create_dataset()
     input_dataset_buffer = acl.create_data_buffer(data, data_size)
     _, ret = acl.mdl.add_dataset_buffer(self.input_dataset,
                                         input_dataset_buffer)
     if ret:
         ret = acl.destroy_data_buffer(self.input_dataset)
         check_ret("acl.destroy_data_buffer", ret)
Exemplo n.º 11
0
    def _release_databuffer(self, data_buffer, free_memory=False):
        if free_memory:
            data_addr = acl.get_data_buffer_addr(data_buffer)
            if data_addr:
                acl.rt.free(data_addr)

        ret = acl.destroy_data_buffer(data_buffer)
        if ret != const.ACL_ERROR_NONE:
            log_error("Destroy data buffer error ", ret)
Exemplo n.º 12
0
 def _gen_output_dataset(self, ouput_num):
     log_info("[Model] create model output dataset:")
     dataset = acl.mdl.create_dataset()
     for i in range(ouput_num):
         #malloc device memory for output
         size = acl.mdl.get_output_size_by_index(self._model_desc, i)
         buf, ret = acl.rt.malloc(size, const.ACL_MEM_MALLOC_NORMAL_ONLY)
         utils.check_ret("acl.rt.malloc", ret)
         #crate oputput data buffer
         dataset_buffer = acl.create_data_buffer(buf, size)
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         log_info("malloc output %d, size %d" % (i, size))
         if ret:
             acl.rt.free(buf)
             acl.destroy_data_buffer(dataset_buffer)
             utils.check_ret("acl.destroy_data_buffer", ret)
     self._output_dataset = dataset
     log_info("Create model output dataset success")
Exemplo n.º 13
0
 def _gen_output_dataset(self, size):
     # print("[Model] create model output dataset:")
     dataset = acl.mdl.create_dataset()
     for i in range(size):
         # allocate device memory for each output
         size = acl.mdl.get_output_size_by_index(self.model_desc, i)
         buffer, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY)
         check_ret("acl.rt.malloc", ret)
         # create output data buffer structure, fill allocated memory into the data buffer
         dataset_buffer = acl.create_data_buffer(buffer, size)
         # add data buffer to output dataset
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         # print("malloc output %d, size %d"%(i, size))
         if ret:
             # release resource if failed
             acl.rt.free(buffer)
             acl.destroy_data_buffer(dataset)
             check_ret("acl.destroy_data_buffer", ret)
     self.output_dataset = dataset
Exemplo n.º 14
0
 def _gen_output_dataset(self, size):
     print("[Model] create model output dataset:")
     dataset = acl.mdl.create_dataset()
     for i in range(size):
         #为每个输出申请device内存
         size = acl.mdl.get_output_size_by_index(self.model_desc, i)
         buffer, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY)
         check_ret("acl.rt.malloc", ret)
         #创建输出的data buffer结构,将申请的内存填入data buffer
         dataset_buffer = acl.create_data_buffer(buffer, size)
         #将data buffer加入输出dataset
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         print("malloc output %d, size %d" % (i, size))
         if ret:
             #如果失败,则释放资源
             acl.rt.free(buffer)
             acl.destroy_data_buffer(dataset)
             check_ret("acl.destroy_data_buffer", ret)
     self.output_dataset = dataset
     print("[Model] create model output dataset success")
Exemplo n.º 15
0
 def _gen_input_dataset(self, dvpp_output_buffer, dvpp_output_size):
     print("[Model] create model input dataset:")
     self.input_dataset = acl.mdl.create_dataset()
     input_dataset_buffer = acl.create_data_buffer(dvpp_output_buffer,
                                                   dvpp_output_size)
     _, ret = acl.mdl.add_dataset_buffer(self.input_dataset,
                                         input_dataset_buffer)
     if ret:
         ret = acl.destroy_data_buffer(input_dataset_buffer)
         check_ret("acl.destroy_data_buffer", ret)
     print("[Model] create model input dataset success")
Exemplo n.º 16
0
 def _gen_input_dataset(self, input_buffer, input_size):
     """ Create input dataset buffer for inference model.
     input_buffer: The memory holds the input data on device.
     input_size:   The size of device memory holding the input data.
     """
     self.input_dataset = acl.mdl.create_dataset()
     input_dataset_buffer = acl.create_data_buffer(input_buffer, input_size)
     _, ret = acl.mdl.add_dataset_buffer(self.input_dataset,
                                         input_dataset_buffer)
     if ret:
         ret = acl.destroy_data_buffer(input_dataset_buffer)
         check_ret("acl.destroy_data_buffer", ret)
Exemplo n.º 17
0
 def _release_dataset(self):
     for dataset in [self.input_dataset, self.output_dataset]:
         if not dataset:
             continue
         num = acl.mdl.get_dataset_num_buffers(dataset)
         for i in range(num):
             data_buf = acl.mdl.get_dataset_buffer(dataset, i)
             if data_buf:
                 ret = acl.destroy_data_buffer(data_buf)
                 check_ret("acl.destroy_data_buffer", ret)
         ret = acl.mdl.destroy_dataset(dataset)
         check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 18
0
 def _destroy_databuffer(self):
     for dataset in [self.load_input_dataset, self.load_output_dataset]:
         if not dataset:
             continue
         number = acl.mdl.get_dataset_num_buffers(dataset)
         for i in range(number):
             data_buf = acl.mdl.get_dataset_buffer(dataset, i)
             if data_buf:
                 ret = acl.destroy_data_buffer(data_buf)
                 check_ret("acl.destroy_data_buffer", ret)
         ret = acl.mdl.destroy_dataset(dataset)
         check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 19
0
 def _release_dataset(self, dataset):
     if not dataset:
         return
     num = acl.mdl.get_dataset_num_buffers(dataset)
     for i in range(num):
         data_buf = acl.mdl.get_dataset_buffer(dataset, i)
         if data_buf:
             ret = acl.destroy_data_buffer(data_buf)
             if ret != ACL_ERROR_NONE:
                 print("Destroy data buffer error ", ret)
     ret = acl.mdl.destroy_dataset(dataset)
     if ret != ACL_ERROR_NONE:
         print("Destroy data buffer error ", ret)
Exemplo n.º 20
0
 def _gen_output_dataset(self, size):
     print("[Model] create model output dataset:")
     dataset = acl.mdl.create_dataset()
     for i in range(size):
         # create output memory
         temp_buffer_size = acl.mdl.\
             get_output_size_by_index(self.model_desc, i)
         temp_buffer, ret = acl.rt.malloc(temp_buffer_size,
                                          ACL_MEM_MALLOC_NORMAL_ONLY)
         check_ret("acl.rt.malloc", ret)
         #create output data buffer
         dataset_buffer = acl.create_data_buffer(temp_buffer,
                                                 temp_buffer_size)
         #add data buffer to output dataset
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         if ret:
             #free resource
             acl.rt.free(temp_buffer)
             acl.destroy_data_buffer(dataset)
             check_ret("acl.destroy_data_buffer", ret)
     self.output_dataset = dataset
     print("[Model] create model output dataset success")
     return
Exemplo n.º 21
0
 def _release_dataset(self, ):
     for dataset in [self.input_dataset, self.output_dataset]:
         if not dataset:
             continue
         # 获取data buffer个数
         number_t = acl.mdl.get_dataset_num_buffers(dataset)
         for i in range(number_t):
             data_buf = acl.mdl.get_dataset_buffer(dataset, i)
             if data_buf:
                 ret = acl.destroy_data_buffer(data_buf)
                 check_ret("acl.destroy_data_buffer", ret)
         # 释放dataset
         ret = acl.mdl.destroy_dataset(dataset)
         check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 22
0
 def _destroy_dataset_and_databuf(self, ):
     while self.dataset_list:
         dataset = self.dataset_list.pop()
         for temp in dataset:
             num_temp = acl.mdl.get_dataset_num_buffers(temp)
             for i in range(num_temp):
                 data_buf_temp = acl.mdl.get_dataset_buffer(temp, i)
                 if data_buf_temp:
                     data = acl.get_data_buffer_addr(data_buf_temp)
                     ret = acl.rt.free(data)
                     check_ret("acl.rt.free", ret)
                     ret = acl.destroy_data_buffer(data_buf_temp)
                     check_ret("acl.destroy_data_buffer", ret)
             ret = acl.mdl.destroy_dataset(temp)
             check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 23
0
 def _release_dataset(self):
     for dataset in [self.input_dataset, self.output_data]:
         if not dataset:
             continue
         # 获取data buffer的个数并逐一释放
         num = acl.mdl.get_dataset_num_buffers(dataset)
         for i in range(num):
             data_buf = acl.mdl.get_dataset_buffer(dataset, i)
             if data_buf:
                 data = acl.get_data_buffer_addr(data_buf)
                 ret = acl.rt.free(data)
                 check_ret("acl.rt.free", ret)
                 ret = acl.destroy_data_buffer(data_buf)
                 check_ret("acl.destroy_data_buffer", ret)
         ret = acl.mdl.destroy_dataset(dataset)
         check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 24
0
    def _load_output_data(self):
        output_data = acl.mdl.create_dataset()
        for i in range(self.output_num):
            # check temp_buffer dtype
            temp_buffer_size = acl.mdl.get_output_size_by_index(
                self.model_desc, i)
            temp_buffer, ret = acl.rt.malloc(
                temp_buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY)
            check_ret("acl.rt.malloc", ret)

            data_buf = acl.create_data_buffer(temp_buffer, temp_buffer_size)
            _, ret = acl.mdl.add_dataset_buffer(output_data, data_buf)
            if ret != ACL_SUCCESS:
                ret = acl.destroy_data_buffer(data_buf)
                check_ret("acl.destroy_data_buffer", ret)
        return output_data
Exemplo n.º 25
0
    def _release_dataset(self):
        """ Release dataset buffer for both input and output. """
        for dataset in [self.input_dataset, self.output_data]:
            if not dataset:
                continue

            num = acl.mdl.get_dataset_num_buffers(dataset)
            for i in range(num):
                data_buf = acl.mdl.get_dataset_buffer(dataset, i)
                if data_buf:
                    data = acl.get_data_buffer_addr(data_buf)
                    ret = acl.rt.free(data)
                    check_ret("acl.rt.free", ret)
                    ret = acl.destroy_data_buffer(data_buf)
                    check_ret("acl.destroy_data_buffer", ret)
            ret = acl.mdl.destroy_dataset(dataset)
            check_ret("acl.mdl.destroy_dataset", ret)
Exemplo n.º 26
0
    def _gen_dataset(self, type_str="input"):
        dataset = acl.mdl.create_dataset()

        temp_dataset = None
        if type_str == "in":
            self.load_input_dataset = dataset
            temp_dataset = self.input_data
        else:
            self.load_output_dataset = dataset
            temp_dataset = self.output_data

        for item in temp_dataset:
            data = acl.create_data_buffer(item["buffer"], item["size"])
            _, ret = acl.mdl.add_dataset_buffer(dataset, data)

            if ret != ACL_SUCCESS:
                ret = acl.destroy_data_buffer(data)
                check_ret("acl.destroy_data_buffer", ret)
Exemplo n.º 27
0
 def _gen_output_dataset(self, size):
     """ Create Output dataset buffer.
     size: The number of elements in input batch (minibatch size).
     """
     dataset = acl.mdl.create_dataset()
     for i in range(size):
         temp_buffer_size = acl.mdl.get_output_size_by_index(
             self.model_desc, i)
         temp_buffer, ret = acl.rt.malloc(temp_buffer_size,
                                          ACL_MEM_MALLOC_NORMAL_ONLY)
         check_ret("acl.rt.malloc", ret)
         dataset_buffer = acl.create_data_buffer(temp_buffer,
                                                 temp_buffer_size)
         _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
         if ret:
             ret = acl.destroy_data_buffer(dataset_buffer)
             check_ret("acl.destroy_data_buffer", ret)
     self.output_data = dataset
Exemplo n.º 28
0
    def _gen_output_dataset(self, size):
        print("[Model] create model output dataset:")
        dataset = acl.mdl.create_dataset()
        for i in range(size):
            temp_buffer_size = acl.mdl.\
                get_output_size_by_index(self.model_desc, i)
            temp_buffer, ret = acl.rt.malloc(temp_buffer_size,
                                             ACL_MEM_MALLOC_NORMAL_ONLY)
            check_ret("acl.rt.malloc", ret)
            self.device_outputs.append(temp_buffer)
            dataset_buffer = acl.create_data_buffer(temp_buffer,
                                                    temp_buffer_size)

            _, ret = acl.mdl.add_dataset_buffer(dataset, dataset_buffer)
            if ret:
                ret = acl.destroy_data_buffer(dataset_buffer)
                check_ret("acl.destroy_data_buffer", ret)
        self.output_data = dataset
        print("[Model] create model output dataset success")