Expert distribution recording without overhead for EPLB (#4957)
This commit is contained in:
@@ -46,7 +46,19 @@ from importlib.util import find_spec
|
||||
from io import BytesIO
|
||||
from multiprocessing.reduction import ForkingPickler
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, List, Optional, Protocol, Set, Tuple, Union
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
Generic,
|
||||
List,
|
||||
Optional,
|
||||
Protocol,
|
||||
Set,
|
||||
Tuple,
|
||||
TypeVar,
|
||||
Union,
|
||||
)
|
||||
|
||||
import numpy as np
|
||||
import psutil
|
||||
@@ -2126,3 +2138,25 @@ def load_json_config(data: str):
|
||||
|
||||
def dispose_tensor(x: torch.Tensor):
|
||||
x.set_(torch.empty((0,), device=x.device, dtype=x.dtype))
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class Withable(Generic[T]):
|
||||
def __init__(self):
|
||||
self._value: Optional[T] = None
|
||||
|
||||
@property
|
||||
def value(self) -> T:
|
||||
return self._value
|
||||
|
||||
@contextmanager
|
||||
def with_value(self, new_value: T):
|
||||
assert self._value is None
|
||||
self._value = new_value
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
assert self._value is new_value
|
||||
self._value = None
|
||||
|
||||
Reference in New Issue
Block a user