Spaces:
Runtime error
Runtime error
Factor Studios
commited on
Update torch_vgpu.py
Browse files- torch_vgpu.py +24 -11
torch_vgpu.py
CHANGED
|
@@ -15,18 +15,31 @@ def init_vgpu_backend():
|
|
| 15 |
global VGPU_BACKEND_INITIALIZED
|
| 16 |
try:
|
| 17 |
if not VGPU_BACKEND_INITIALIZED:
|
| 18 |
-
# First
|
| 19 |
-
lib = Library("privateuseone", "DEF")
|
| 20 |
-
lib.define("custom_op(Tensor self) -> Tensor")
|
| 21 |
-
|
| 22 |
-
@impl("privateuseone", "custom_op", "Tensor")
|
| 23 |
-
def custom_op_impl(tensor):
|
| 24 |
-
return tensor.clone()
|
| 25 |
-
|
| 26 |
-
# Then rename the backend
|
| 27 |
torch.utils.rename_privateuse1_backend("vgpu")
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
torch.utils.generate_methods_for_privateuse1_backend(
|
| 31 |
for_tensor=True,
|
| 32 |
for_module=True,
|
|
@@ -85,7 +98,7 @@ class VGPUDevice:
|
|
| 85 |
raise RuntimeError("VGPU backend not properly initialized")
|
| 86 |
|
| 87 |
# Create device with explicit index
|
| 88 |
-
self._device = torch.device("vgpu")
|
| 89 |
|
| 90 |
# Store this instance for reuse
|
| 91 |
VGPUDevice._VGPU_INSTANCES[self.device_name] = self
|
|
|
|
| 15 |
global VGPU_BACKEND_INITIALIZED
|
| 16 |
try:
|
| 17 |
if not VGPU_BACKEND_INITIALIZED:
|
| 18 |
+
# First rename privateuse1 to vgpu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
torch.utils.rename_privateuse1_backend("vgpu")
|
| 20 |
|
| 21 |
+
# Create library for the backend
|
| 22 |
+
lib = Library("vgpu", "IMPL")
|
| 23 |
+
|
| 24 |
+
# Register essential operations for the backend
|
| 25 |
+
@impl(lib, "aten::empty.memory_format")
|
| 26 |
+
def empty_impl(size, dtype=None, layout=None, device=None, pin_memory=None, memory_format=None):
|
| 27 |
+
# Create empty tensor on CPU first, will be moved to vGPU storage later
|
| 28 |
+
return torch.empty(size, dtype=dtype, device='cpu')
|
| 29 |
+
|
| 30 |
+
@impl(lib, "aten::copy.from")
|
| 31 |
+
def copy_impl(self, src, non_blocking=False):
|
| 32 |
+
# Handle tensor copying between devices
|
| 33 |
+
return torch.tensor(src.cpu().numpy(), device='cpu')
|
| 34 |
+
|
| 35 |
+
@impl(lib, "aten::_to_copy")
|
| 36 |
+
def to_impl(self, dtype=None, device=None, non_blocking=False, copy=False):
|
| 37 |
+
# Handle tensor device transfer
|
| 38 |
+
if device and str(device).startswith("vgpu"):
|
| 39 |
+
return self.cpu().clone()
|
| 40 |
+
return self.clone()
|
| 41 |
+
|
| 42 |
+
# Generate all methods for our backend
|
| 43 |
torch.utils.generate_methods_for_privateuse1_backend(
|
| 44 |
for_tensor=True,
|
| 45 |
for_module=True,
|
|
|
|
| 98 |
raise RuntimeError("VGPU backend not properly initialized")
|
| 99 |
|
| 100 |
# Create device with explicit index
|
| 101 |
+
self._device = torch.device("vgpu:0")
|
| 102 |
|
| 103 |
# Store this instance for reuse
|
| 104 |
VGPUDevice._VGPU_INSTANCES[self.device_name] = self
|