44# DeepSpeed Team
55
66from abc import ABC , abstractmethod
7+ from typing import Optional
8+
9+ import torch
10+
11+ from deepspeed .inference .v2 .logging import inference_logger
712
813
914class DSKernelBase (ABC ):
1015
16+ _workspace : Optional [torch .Tensor ] = None
17+
1118 @abstractmethod
1219 def __init__ (self , * args , ** kwargs ):
1320 """
@@ -30,3 +37,50 @@ def __call__(self, *args, **kwargs):
3037 should be performed here.
3138 """
3239 raise NotImplementedError ()
40+
41+ def requested_workspace_size (self ) -> int :
42+ """
43+ Return the requested workspace size in bytes.
44+
45+ This should be overloaded if the kernel requires a workspace.
46+
47+ Returns:
48+ int: Number of bytes necessary.
49+ """
50+ return 0
51+
52+ def get_workspace (self , bytes : int ) -> torch .Tensor :
53+ """
54+ Return the data pointer to the scratchpad memory.
55+
56+ Args:
57+ bytes (int): Number of bytes necessary.
58+
59+ Raises:
60+ RuntimeError: If the workspace is not allocated.
61+ ValueError: If the workspace is not large enough.
62+ """
63+ if DSKernelBase ._workspace is None :
64+ raise RuntimeError ("Workspace not allocated" )
65+ if DSKernelBase ._workspace .numel () < bytes :
66+ raise ValueError ("Workspace too small" )
67+ return DSKernelBase ._workspace
68+
69+ @staticmethod
70+ def create_workspace (bytes : int ) -> int :
71+ """
72+ Create a workspace of the requested size.
73+
74+ Args:
75+ bytes (int): Number of bytes necessary.
76+
77+ Raises:
78+ RuntimeError: If the workspace is already allocated.
79+ """
80+ if DSKernelBase ._workspace is not None :
81+ raise RuntimeError ("Workspace already allocated" )
82+
83+ if bytes > 0 :
84+ inference_logger ().info (f"Allocating { bytes } bytes of workspace" )
85+
86+ DSKernelBase ._workspace = torch .empty ((bytes , ), dtype = torch .uint8 )
0 commit comments