2023-08-04 08:40:52 +08:00
|
|
|
import unittest
|
|
|
|
import multiprocessing.shared_memory as shared_memory
|
2023-09-22 09:16:42 +08:00
|
|
|
from tinygrad.helpers import CI
|
2023-08-04 08:40:52 +08:00
|
|
|
from tinygrad.tensor import Tensor, Device
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
class TestRawShmBuffer(unittest.TestCase):
|
|
|
|
def test_e2e(self):
|
|
|
|
t = Tensor.randn(2, 2, 2).realize()
|
|
|
|
|
|
|
|
# copy to shm
|
|
|
|
shm_name = (s := shared_memory.SharedMemory(create=True, size=t.nbytes())).name
|
|
|
|
s.close()
|
2023-11-19 03:41:58 +08:00
|
|
|
t_shm = t.to(f"disk:shm:{shm_name}").realize()
|
2023-08-04 08:40:52 +08:00
|
|
|
|
|
|
|
# copy from shm
|
|
|
|
t2 = t_shm.to(Device.DEFAULT).realize()
|
|
|
|
|
|
|
|
assert np.allclose(t.numpy(), t2.numpy())
|
|
|
|
s.unlink()
|
|
|
|
|
|
|
|
@unittest.skipIf(CI, "CI doesn't like big shared memory")
|
|
|
|
def test_e2e_big(self):
|
|
|
|
t = Tensor.randn(2048, 2048, 8).realize()
|
|
|
|
|
|
|
|
# copy to shm
|
|
|
|
shm_name = (s := shared_memory.SharedMemory(create=True, size=t.nbytes())).name
|
|
|
|
s.close()
|
2023-11-19 03:41:58 +08:00
|
|
|
t_shm = t.to(f"disk:shm:{shm_name}").realize()
|
2023-08-04 08:40:52 +08:00
|
|
|
|
|
|
|
# copy from shm
|
|
|
|
t2 = t_shm.to(Device.DEFAULT).realize()
|
|
|
|
|
|
|
|
assert np.allclose(t.numpy(), t2.numpy())
|
|
|
|
s.unlink()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|