forked from pytorch/ao
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (40 loc) · 1.64 KB
/
Copy pathutils.py
File metadata and controls
48 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import torch
from typing import Union, Tuple
from dataclasses import dataclass
"""
Base class for different LayoutType, should not be instantiated directly
used to allow users to pass around configurations for the layout tensor, e.g. inner_k_tiles
for int4 tensor core tiled layout
Note: layout is an abstraction not only for custom data representation, it is also used for how the
layout interacts with different operators, e.g. the same data representation can have different
behaviors when running the same operator, e.g. transpose, quantized_linear.
"""
@dataclass(frozen=True)
class LayoutType:
def pre_process(self, input: torch.Tensor) -> torch.Tensor:
return input
def post_process(self, input: torch.Tensor) -> torch.Tensor:
return input
def __repr__(self):
return f"{self.__class__.__name__}({self.extra_repr()})"
def extra_repr(self) -> str:
return ""
"""
Plain LayoutType, the most basic LayoutType, also has no extra metadata, will typically be the default
"""
@dataclass(frozen=True)
class PlainLayoutType(LayoutType):
pass
def is_device(target_device_str: str, device: Union[str, torch.device]):
return torch.device(device).type == target_device_str
def get_out_shape(input_shape: Tuple[int], weight_shape: Tuple[int]) -> Tuple[int, int]:
"""Returns the unflattened shape of the input tensor.
Args:
input_shape: The input tensor shape possibly more than 2 dimensions
weight_shape: The weight tensor shape.
Returns:
The unflattened shape of the input tensor.
"""
out_dim = weight_shape[0]
inpt_dims = input_shape[:-1]
return (*inpt_dims, out_dim)