misc.py 1.8 KB
Newer Older
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
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# See LICENSE for license information.
"""
This module provides additional enum and utilities for quantizing tensors in JAX.
"""
from dataclasses import dataclass
from enum import Enum

from transformer_engine_jax import JAXX_Quantize_Layout

__all__ = [
    "QuantizeLayout",
]


@dataclass(frozen=True)
class QuantizeLayout(Enum):
    "Wrapper for JAXX_Quantize_Layout"

    ROWWISE = JAXX_Quantize_Layout.ROWWISE
    COLWISE = JAXX_Quantize_Layout.COLWISE
    ROWWISE_COLWISE = JAXX_Quantize_Layout.ROWWISE_COLWISE

    @property
    def has_rowwise(self) -> bool:
        """If the layout has the rowwise component"""
        return self.value in (JAXX_Quantize_Layout.ROWWISE, JAXX_Quantize_Layout.ROWWISE_COLWISE)

    @property
    def has_colwise(self) -> bool:
        """If the layout has the colwise component"""
        return self.value in (JAXX_Quantize_Layout.COLWISE, JAXX_Quantize_Layout.ROWWISE_COLWISE)

    @property
    def is_rowwise_colwise(self) -> bool:
        """If layout is both rowwise and colwise"""
        return self.value == JAXX_Quantize_Layout.ROWWISE_COLWISE

    @property
    def is_rowwise_only(self) -> bool:
        """If layout is rowwise only"""
        return self.value == JAXX_Quantize_Layout.ROWWISE

    @property
    def is_colwise_only(self) -> bool:
        """If layout is colwise only"""
        return self.value == JAXX_Quantize_Layout.COLWISE

    def __eq__(self, other):
        """Compare this quantize layout with another.

        Args:
            other: The other quantize layout to compare with

        Returns:
            True if the modes are equal, False otherwise
        """
        if not isinstance(other, QuantizeLayout):
            return False
        return self.value == other.value