Module furiosa.quantizer.furiosa_sdk_quantizer.ir.spec

Expand source code
from typing import Dict, List, Optional

from furiosa_sdk_quantizer.ir.common.operator import HeightWidth, Padding, HorizontalPadding


class Spec:
    def __init__(self, name: str, operator_spec: "OperatorSpec"):
        self.name = name
        self.option = operator_spec

    def kind(self):
        raise NotImplementedError()

    def as_dict(self) -> Dict[str, any]:
        return {
            'name': self.name,
            'option': {
                # quantizer only supports 'OperatorSpec'.
                'Operator': self.option.as_dict(),
            }
        }


class OperatorSpec:
    def kind(self):
        raise NotImplementedError()

    def as_dict(self) -> Dict[str, any]:
        return {self.kind(): dict(map(lambda item: self._handle_nested_spec(*item), vars(self).items()))}

    @staticmethod
    def _handle_nested_spec(k, v):
        if hasattr(v, 'as_dict'):
            return k, getattr(v, 'as_dict')()
        else:
            return k, v


class PaddingSpecCustom:
    def __init__(self, padding: Padding):
        self.Custom = padding


class Conv2d(OperatorSpec):
    def kind(self):
        return 'Conv2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, input_channel: int, output_channel: int, groups: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.dilation = dilation
        self.batch = batch
        self.input_channel = input_channel
        self.output_channel = output_channel
        self.groups = groups
        self.padding_spec = PaddingSpecCustom(padding)


class TrasnposeConv(Conv2d):
    def kind(self):
        return 'TransposeConv'


class MaxPool2d(OperatorSpec):
    def kind(self):
        return 'MaxPool2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, channel: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.dilation = dilation
        self.batch = batch
        self.channel = channel
        self.padding_spec = PaddingSpecCustom(padding)


class AveragePool2d(OperatorSpec):
    def kind(self):
        return 'AveragePool2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, channel: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.batch = batch
        self.channel = channel
        self.dilation = dilation
        self.padding_spec = PaddingSpecCustom(padding)


class Gemm(OperatorSpec):
    def kind(self):
        return 'Gemm'

    def __init__(self, alpha: float, beta: float, m: int, k: int, n: int):
        self.alpha = alpha
        self.beta = beta
        self.m = m
        self.k = k
        self.n = n


class MatMul(OperatorSpec):
    def kind(self):
        return 'MatMul'

    def __init__(self, lhs_shape: List[int], rhs_shape: List[int]):
        self.lhs_shape = lhs_shape
        self.rhs_shape = rhs_shape


class DepthToSpace(OperatorSpec):
    def kind(self):
        return 'DepthToSpace'

    def __init__(self, batch: int, height: int, width: int, channel: int, block_size: int, mode: str):
        self.batch = batch
        self.height = height
        self.width = width
        self.channel = channel
        self.block_size = block_size
        self.mode = mode


class Resize(OperatorSpec):
    def kind(self):
        return 'Resize'

    def __init__(self, shape: List[int], roi: List[int], scales: List[float], sizes: List[int]):
        self.shape = shape
        self.roi = roi
        self.scales = scales
        self.sizes = sizes


class Add(OperatorSpec):
    def kind(self):
        return 'Add'

    def __init__(self, shape: List[int]):
        self.shape = shape


class Sub(Add):
    def kind(self):
        return 'Sub'


class Mul(Add):
    def kind(self):
        return 'Mul'


class Div(Add):
    def kind(self):
        return 'Div'


class Exp(Add):
    def kind(self):
        return 'Exp'


class Sigmoid(Add):
    def kind(self):
        return 'Sigmoid'


class Softplus(OperatorSpec):
    def kind(self):
        return 'Softplus'

    def __init__(self, input_shape: List[int]):
        self.input_shape = input_shape


class Gelu(Add):
    def kind(self):
        return 'Gelu'


class ReduceMean(OperatorSpec):
    def kind(self):
        return 'ReduceMean'

    def __init__(self, shape: List[int], axes: List[int]):
        self.shape = shape
        self.axes = axes


class ReduceSum(ReduceMean):
    def kind(self):
        return 'ReduceSum'


class ReduceL2(ReduceMean):
    def kind(self):
        return 'ReduceL2'


class Squeeze(ReduceMean):
    def kind(self):
        return 'Squeeze'


class Unsqueeze(ReduceMean):
    def kind(self):
        return 'Unsqueeze'


class Reshape(OperatorSpec):
    def kind(self):
        return 'Reshape'

    def __init__(self, input_shape: List[int], output_shape: List[int]):
        self.input_shape = input_shape
        self.output_shape = output_shape


class Expand(Reshape):
    def kind(self):
        return 'Expand'


class Concatenation(OperatorSpec):
    def kind(self):
        return 'Concatenation'

    def __init__(self, tensors: List[List[int]], axis: int):
        self.tensors = tensors
        self.axis = axis


class Transpose(OperatorSpec):
    def kind(self):
        return 'Transpose'

    def __init__(self, shape: List[int], permutation: List[int]):
        self.shape = shape
        self.permutation = permutation


class Slice(OperatorSpec):
    def kind(self):
        return 'Slice'

    def __init__(self, shape: List[int], offset: List[int]):
        self.shape = shape
        self.offset = offset


class Flatten(OperatorSpec):
    def kind(self):
        return 'Flatten'

    def __init__(self, shape: List[int], axis: int):
        self.shape = shape
        # The field `Axis` isn't in npu-tools.
        self.axis = axis


class Pad(OperatorSpec):
    def kind(self):
        return 'Pad'

    def __init__(self, shape: List[int], pad: List[HorizontalPadding]):
        self.shape = shape
        self.pad = pad


class Split(OperatorSpec):
    def kind(self):
        return 'Split'

    def __init__(self, shape: List[int], split: List[int], axis: int):
        self.shape = shape
        self.split = split
        self.axis = axis


class Softmax(OperatorSpec):
    def kind(self):
        return 'Softmax'

    def __init__(self, input_shape: List[int], beta: float, axis: int):
        self.input_shape = input_shape
        self.beta = beta
        self.axis = axis


class Clip(OperatorSpec):
    def kind(self):
        return 'Clip'

    def __init__(self, input_shape: List[int], min: Optional[float] = None, max: Optional[float] = None):
        self.input_shape = input_shape
        self.min = min
        self.max = max


class LpNorm(OperatorSpec):
    def kind(self):
        return 'LpNorm'

    def __init__(self, input_shape: List[int], p: int, axis: int):
        self.input_shape = input_shape
        self.p = p
        self.axis = axis


class LayerNorm(OperatorSpec):
    def kind(self):
        return 'LayerNorm'

    def __init__(self, input_shape: List[int], eps: float):
        self.input_shape = input_shape
        self.eps = eps

Classes

class Add (shape: List[int])
Expand source code
class Add(OperatorSpec):
    def kind(self):
        return 'Add'

    def __init__(self, shape: List[int]):
        self.shape = shape

Ancestors

Subclasses

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Add'
class AveragePool2d (input: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, kernel: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, stride: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, dilation: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, batch: int, channel: int, padding: furiosa_sdk_quantizer.ir.common.operator.Padding)
Expand source code
class AveragePool2d(OperatorSpec):
    def kind(self):
        return 'AveragePool2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, channel: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.batch = batch
        self.channel = channel
        self.dilation = dilation
        self.padding_spec = PaddingSpecCustom(padding)

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'AveragePool2d'
class Clip (input_shape: List[int], min: Union[float, NoneType] = None, max: Union[float, NoneType] = None)
Expand source code
class Clip(OperatorSpec):
    def kind(self):
        return 'Clip'

    def __init__(self, input_shape: List[int], min: Optional[float] = None, max: Optional[float] = None):
        self.input_shape = input_shape
        self.min = min
        self.max = max

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Clip'
class Concatenation (tensors: List[List[int]], axis: int)
Expand source code
class Concatenation(OperatorSpec):
    def kind(self):
        return 'Concatenation'

    def __init__(self, tensors: List[List[int]], axis: int):
        self.tensors = tensors
        self.axis = axis

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Concatenation'
class Conv2d (input: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, kernel: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, stride: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, dilation: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, batch: int, input_channel: int, output_channel: int, groups: int, padding: furiosa_sdk_quantizer.ir.common.operator.Padding)
Expand source code
class Conv2d(OperatorSpec):
    def kind(self):
        return 'Conv2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, input_channel: int, output_channel: int, groups: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.dilation = dilation
        self.batch = batch
        self.input_channel = input_channel
        self.output_channel = output_channel
        self.groups = groups
        self.padding_spec = PaddingSpecCustom(padding)

Ancestors

Subclasses

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Conv2d'
class DepthToSpace (batch: int, height: int, width: int, channel: int, block_size: int, mode: str)
Expand source code
class DepthToSpace(OperatorSpec):
    def kind(self):
        return 'DepthToSpace'

    def __init__(self, batch: int, height: int, width: int, channel: int, block_size: int, mode: str):
        self.batch = batch
        self.height = height
        self.width = width
        self.channel = channel
        self.block_size = block_size
        self.mode = mode

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'DepthToSpace'
class Div (shape: List[int])
Expand source code
class Div(Add):
    def kind(self):
        return 'Div'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Div'
class Exp (shape: List[int])
Expand source code
class Exp(Add):
    def kind(self):
        return 'Exp'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Exp'
class Expand (input_shape: List[int], output_shape: List[int])
Expand source code
class Expand(Reshape):
    def kind(self):
        return 'Expand'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Expand'
class Flatten (shape: List[int], axis: int)
Expand source code
class Flatten(OperatorSpec):
    def kind(self):
        return 'Flatten'

    def __init__(self, shape: List[int], axis: int):
        self.shape = shape
        # The field `Axis` isn't in npu-tools.
        self.axis = axis

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Flatten'
class Gelu (shape: List[int])
Expand source code
class Gelu(Add):
    def kind(self):
        return 'Gelu'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Gelu'
class Gemm (alpha: float, beta: float, m: int, k: int, n: int)
Expand source code
class Gemm(OperatorSpec):
    def kind(self):
        return 'Gemm'

    def __init__(self, alpha: float, beta: float, m: int, k: int, n: int):
        self.alpha = alpha
        self.beta = beta
        self.m = m
        self.k = k
        self.n = n

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Gemm'
class LayerNorm (input_shape: List[int], eps: float)
Expand source code
class LayerNorm(OperatorSpec):
    def kind(self):
        return 'LayerNorm'

    def __init__(self, input_shape: List[int], eps: float):
        self.input_shape = input_shape
        self.eps = eps

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'LayerNorm'
class LpNorm (input_shape: List[int], p: int, axis: int)
Expand source code
class LpNorm(OperatorSpec):
    def kind(self):
        return 'LpNorm'

    def __init__(self, input_shape: List[int], p: int, axis: int):
        self.input_shape = input_shape
        self.p = p
        self.axis = axis

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'LpNorm'
class MatMul (lhs_shape: List[int], rhs_shape: List[int])
Expand source code
class MatMul(OperatorSpec):
    def kind(self):
        return 'MatMul'

    def __init__(self, lhs_shape: List[int], rhs_shape: List[int]):
        self.lhs_shape = lhs_shape
        self.rhs_shape = rhs_shape

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'MatMul'
class MaxPool2d (input: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, kernel: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, stride: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, dilation: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, batch: int, channel: int, padding: furiosa_sdk_quantizer.ir.common.operator.Padding)
Expand source code
class MaxPool2d(OperatorSpec):
    def kind(self):
        return 'MaxPool2d'

    def __init__(self, input: HeightWidth, kernel: HeightWidth, stride: HeightWidth, dilation: HeightWidth,
                 batch: int, channel: int, padding: Padding):
        self.input = input
        self.kernel = kernel
        self.stride = stride
        self.dilation = dilation
        self.batch = batch
        self.channel = channel
        self.padding_spec = PaddingSpecCustom(padding)

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'MaxPool2d'
class Mul (shape: List[int])
Expand source code
class Mul(Add):
    def kind(self):
        return 'Mul'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Mul'
class OperatorSpec
Expand source code
class OperatorSpec:
    def kind(self):
        raise NotImplementedError()

    def as_dict(self) -> Dict[str, any]:
        return {self.kind(): dict(map(lambda item: self._handle_nested_spec(*item), vars(self).items()))}

    @staticmethod
    def _handle_nested_spec(k, v):
        if hasattr(v, 'as_dict'):
            return k, getattr(v, 'as_dict')()
        else:
            return k, v

Subclasses

Methods

def as_dict(self) ‑> Dict[str, ]
Expand source code
def as_dict(self) -> Dict[str, any]:
    return {self.kind(): dict(map(lambda item: self._handle_nested_spec(*item), vars(self).items()))}
def kind(self)
Expand source code
def kind(self):
    raise NotImplementedError()
class Pad (shape: List[int], pad: List[furiosa_sdk_quantizer.ir.common.operator.HorizontalPadding])
Expand source code
class Pad(OperatorSpec):
    def kind(self):
        return 'Pad'

    def __init__(self, shape: List[int], pad: List[HorizontalPadding]):
        self.shape = shape
        self.pad = pad

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Pad'
class PaddingSpecCustom (padding: furiosa_sdk_quantizer.ir.common.operator.Padding)
Expand source code
class PaddingSpecCustom:
    def __init__(self, padding: Padding):
        self.Custom = padding
class ReduceL2 (shape: List[int], axes: List[int])
Expand source code
class ReduceL2(ReduceMean):
    def kind(self):
        return 'ReduceL2'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'ReduceL2'
class ReduceMean (shape: List[int], axes: List[int])
Expand source code
class ReduceMean(OperatorSpec):
    def kind(self):
        return 'ReduceMean'

    def __init__(self, shape: List[int], axes: List[int]):
        self.shape = shape
        self.axes = axes

Ancestors

Subclasses

Methods

def kind(self)
Expand source code
def kind(self):
    return 'ReduceMean'
class ReduceSum (shape: List[int], axes: List[int])
Expand source code
class ReduceSum(ReduceMean):
    def kind(self):
        return 'ReduceSum'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'ReduceSum'
class Reshape (input_shape: List[int], output_shape: List[int])
Expand source code
class Reshape(OperatorSpec):
    def kind(self):
        return 'Reshape'

    def __init__(self, input_shape: List[int], output_shape: List[int]):
        self.input_shape = input_shape
        self.output_shape = output_shape

Ancestors

Subclasses

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Reshape'
class Resize (shape: List[int], roi: List[int], scales: List[float], sizes: List[int])
Expand source code
class Resize(OperatorSpec):
    def kind(self):
        return 'Resize'

    def __init__(self, shape: List[int], roi: List[int], scales: List[float], sizes: List[int]):
        self.shape = shape
        self.roi = roi
        self.scales = scales
        self.sizes = sizes

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Resize'
class Sigmoid (shape: List[int])
Expand source code
class Sigmoid(Add):
    def kind(self):
        return 'Sigmoid'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Sigmoid'
class Slice (shape: List[int], offset: List[int])
Expand source code
class Slice(OperatorSpec):
    def kind(self):
        return 'Slice'

    def __init__(self, shape: List[int], offset: List[int]):
        self.shape = shape
        self.offset = offset

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Slice'
class Softmax (input_shape: List[int], beta: float, axis: int)
Expand source code
class Softmax(OperatorSpec):
    def kind(self):
        return 'Softmax'

    def __init__(self, input_shape: List[int], beta: float, axis: int):
        self.input_shape = input_shape
        self.beta = beta
        self.axis = axis

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Softmax'
class Softplus (input_shape: List[int])
Expand source code
class Softplus(OperatorSpec):
    def kind(self):
        return 'Softplus'

    def __init__(self, input_shape: List[int]):
        self.input_shape = input_shape

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Softplus'
class Spec (name: str, operator_spec: OperatorSpec)
Expand source code
class Spec:
    def __init__(self, name: str, operator_spec: "OperatorSpec"):
        self.name = name
        self.option = operator_spec

    def kind(self):
        raise NotImplementedError()

    def as_dict(self) -> Dict[str, any]:
        return {
            'name': self.name,
            'option': {
                # quantizer only supports 'OperatorSpec'.
                'Operator': self.option.as_dict(),
            }
        }

Methods

def as_dict(self) ‑> Dict[str, ]
Expand source code
def as_dict(self) -> Dict[str, any]:
    return {
        'name': self.name,
        'option': {
            # quantizer only supports 'OperatorSpec'.
            'Operator': self.option.as_dict(),
        }
    }
def kind(self)
Expand source code
def kind(self):
    raise NotImplementedError()
class Split (shape: List[int], split: List[int], axis: int)
Expand source code
class Split(OperatorSpec):
    def kind(self):
        return 'Split'

    def __init__(self, shape: List[int], split: List[int], axis: int):
        self.shape = shape
        self.split = split
        self.axis = axis

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Split'
class Squeeze (shape: List[int], axes: List[int])
Expand source code
class Squeeze(ReduceMean):
    def kind(self):
        return 'Squeeze'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Squeeze'
class Sub (shape: List[int])
Expand source code
class Sub(Add):
    def kind(self):
        return 'Sub'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Sub'
class Transpose (shape: List[int], permutation: List[int])
Expand source code
class Transpose(OperatorSpec):
    def kind(self):
        return 'Transpose'

    def __init__(self, shape: List[int], permutation: List[int]):
        self.shape = shape
        self.permutation = permutation

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Transpose'
class TrasnposeConv (input: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, kernel: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, stride: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, dilation: furiosa_sdk_quantizer.ir.common.operator.HeightWidth, batch: int, input_channel: int, output_channel: int, groups: int, padding: furiosa_sdk_quantizer.ir.common.operator.Padding)
Expand source code
class TrasnposeConv(Conv2d):
    def kind(self):
        return 'TransposeConv'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'TransposeConv'
class Unsqueeze (shape: List[int], axes: List[int])
Expand source code
class Unsqueeze(ReduceMean):
    def kind(self):
        return 'Unsqueeze'

Ancestors

Methods

def kind(self)
Expand source code
def kind(self):
    return 'Unsqueeze'