Browse Source

feat(lite): add get physic addr interface in lite

GitOrigin-RevId: e5a9eb1999
tags/v1.9.0
Megvii Engine Team 3 years ago
parent
commit
73b518b718
6 changed files with 56 additions and 0 deletions
  1. +5
    -0
      lite/include/lite/global.h
  2. +6
    -0
      lite/lite-c/include/lite-c/global_c.h
  3. +8
    -0
      lite/lite-c/src/global.cpp
  4. +10
    -0
      lite/pylite/megenginelite/global_setting.py
  5. +14
    -0
      lite/pylite/test/test_global.py
  6. +13
    -0
      lite/src/global.cpp

+ 5
- 0
lite/include/lite/global.h View File

@@ -169,6 +169,11 @@ LITE_API bool clear_memory_pair(
void* vir_ptr, void* phy_ptr, LiteDeviceType device,
LiteBackend backend = LiteBackend::LITE_DEFAULT);

/**
* get the physic address by the virtual address in mge.
*/
void* lookup_physic_ptr(void* vir_ptr, LiteDeviceType device, LiteBackend backend);

} // namespace lite

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

+ 6
- 0
lite/lite-c/include/lite-c/global_c.h View File

@@ -175,6 +175,12 @@ LITE_API int LITE_register_memory_pair(
LITE_API int LITE_clear_memory_pair(
void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend backend);

/**
* get the physical by the virtual address pair in mge.
*/
LITE_API int LITE_lookup_physic_ptr(
void* vir_ptr, void** phy_ptr, LiteDeviceType device, LiteBackend backend);

#ifdef __cplusplus
}
#endif


+ 8
- 0
lite/lite-c/src/global.cpp View File

@@ -204,4 +204,12 @@ int LITE_clear_memory_pair(
LITE_CAPI_END();
}

int LITE_lookup_physic_ptr(
void* vir_ptr, void** phy_ptr, LiteDeviceType device, LiteBackend backend) {
LITE_CAPI_BEGIN();
LITE_ASSERT(vir_ptr && phy_ptr, "The ptr pass to vir and phy is nullptr");
*phy_ptr = lite::lookup_physic_ptr(vir_ptr, device, backend);
LITE_CAPI_END();
}

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

+ 10
- 0
lite/pylite/megenginelite/global_setting.py View File

@@ -44,6 +44,7 @@ class _GlobalAPI(_LiteCObjBase):
("LITE_dump_tensor_rt_cache", [c_char_p]),
("LITE_register_memory_pair", [c_void_p, c_void_p, c_size_t, c_int, c_int]),
("LITE_clear_memory_pair", [c_void_p, c_void_p, c_int, c_int]),
("LITE_lookup_physic_ptr", [c_void_p, POINTER(c_void_p), c_int, c_int]),
]


@@ -141,3 +142,12 @@ class LiteGlobal(object):
phy_ptr, c_void_p
), "clear memory pair only accept c_void_p type."
LiteGlobal._api.LITE_clear_memory_pair(vir_ptr, phy_ptr, device, backend)

@staticmethod
def lookup_physic_ptr(vir_ptr, device, backend=LiteBackend.LITE_DEFAULT):
assert isinstance(
vir_ptr, c_void_p
), "lookup physic ptr only accept c_void_p type."
mem = c_void_p()
LiteGlobal._api.LITE_lookup_physic_ptr(vir_ptr, byref(mem), device, backend)
return mem

+ 14
- 0
lite/pylite/test/test_global.py View File

@@ -9,6 +9,7 @@

import os
import unittest
from ctypes import *

import numpy as np

@@ -71,3 +72,16 @@ class TestGlobal(TestShuffleNet):
network.load(model_path)

self.do_forward(network)

def test_set_get_memory_pair(self):
if LiteGlobal.get_device_count(LiteDeviceType.LITE_AX) > 0:
arr1 = np.ones([2, 3])
arr2 = np.ones([2, 3])
vir_ptr = arr1.ctypes.data_as(c_void_p)
phy_ptr = arr2.ctypes.data_as(c_void_p)
LiteGlobal.register_memory_pair(
vir_ptr, phy_ptr, 10, LiteDeviceType.LITE_AX
)
phy_ptr2 = LiteGlobal.lookup_physic_ptr(vir_ptr, LiteDeviceType.LITE_AX)
assert phy_ptr.value == phy_ptr2.value
LiteGlobal.clear_memory_pair(vir_ptr, phy_ptr, LiteDeviceType.LITE_AX)

+ 13
- 0
lite/src/global.cpp View File

@@ -232,6 +232,14 @@ bool lite::clear_memory_pair(
LITE_THROW("clear_memory_pair is not implement yet!");
}

void* lite::lookup_physic_ptr(
void* vir_ptr, LiteDeviceType device, LiteBackend backend) {
LITE_MARK_USED_VAR(vir_ptr);
LITE_MARK_USED_VAR(device);
LITE_MARK_USED_VAR(backend);
LITE_THROW("lookup_physic_ptr is not implement yet!");
}

#else // LITE_BUILD_WITH_MGE
void lite::try_coalesce_all_free_memory() {}

@@ -266,6 +274,11 @@ bool lite::clear_memory_pair(
void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend beckend) {
LITE_THROW("clear_memory_pair is not implement yet!");
}

void* lite::lookup_physic_ptr(
void* vir_ptr, LiteDeviceType device, LiteBackend beckend) {
LITE_THROW("lookup_physic_ptr is not implement yet!");
}
#endif
namespace lite {
REGIST_DECRYPTION_METHOD(


Loading…
Cancel
Save