Ejemplo n.º 1
0
    def testOrtValueDlPack_bool(self):
        numpy_arr_input = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=np.bool)
        ortvalue = onnxrt.OrtValue.ortvalue_from_numpy(numpy_arr_input)
        self.assertEqual(numpy_arr_input.shape, tuple(ortvalue.shape()))
        ptr = ortvalue._ortvalue.data_ptr()

        dlp = ortvalue._ortvalue.to_dlpack()
        self.assertTrue(C.is_dlpack_uint8_tensor(dlp))
        ortvalue2 = C_OrtValue.from_dlpack(dlp, True)
        self.assertEqual(ptr, ortvalue2.data_ptr())
        new_array = ortvalue2.numpy()
        assert_almost_equal(numpy_arr_input, new_array)

        dlp = ortvalue._ortvalue.__dlpack__()
        self.assertTrue(C.is_dlpack_uint8_tensor(dlp))
        ortvalue2 = C_OrtValue.from_dlpack(dlp, True)
        self.assertEqual(ptr, ortvalue2.data_ptr())
        new_array = ortvalue2.numpy()
        assert_almost_equal(numpy_arr_input, new_array)

        device = ortvalue._ortvalue.__dlpack_device__()
        self.assertEqual((1, 0), device)
    def OrtValueVectorDlPackOrtValue(self,
                                     my_to_tensor,
                                     tensor_type,
                                     device,
                                     dtype=np.float32):
        narrays = [
            np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=dtype),
            np.array([[6.0, 7.0], [8.0, 9.0], [1.0, 6.0]], dtype=dtype)
        ]
        vect = OrtValueVector()
        ptr = []
        for a in narrays:
            ortvalue = onnxrt.OrtValue.ortvalue_from_numpy(a, device)
            vect.push_back(ortvalue._ortvalue)
            ptr.append(ortvalue.data_ptr())
        self.assertEqual(len(vect), 2)

        converted_values = vect.to_dlpacks(my_to_tensor)
        self.assertEqual(len(converted_values), len(vect))
        if my_to_tensor is None:
            self.assertIn("PyCapsule", str(type(converted_values[0])))
            converted_values = [
                C_OrtValue.from_dlpack(o, False) for o in converted_values
            ]
        else:
            assert all(
                map(lambda v: isinstance(v, tensor_type), converted_values))

        # We make sure the function does not leak any python object.
        cf = [sys.getrefcount(o) for o in converted_values]
        dummy = [np.array([[0, 1]]), dict(a=3)]
        cf2 = [sys.getrefcount(o) for o in dummy]
        self.assertEqual(cf, cf2)  # it should be [3, 3]

        ptr2 = []
        for av1, v2 in zip(narrays, converted_values):
            ptr2.append(v2.data_ptr())
            if hasattr(v2, 'cpu'):
                av2 = v2.cpu().numpy()
            else:
                av2 = v2.numpy()
            assert_almost_equal(av1, av2)
        self.assertEqual(ptr, ptr2)
 def my_to_tensor(dlpack_structure):
     return C_OrtValue.from_dlpack(dlpack_structure, False)