dmx.compressor.numerical.cast
Classes
|
Simulated numerical cast to a target format subclassing torch.quantization.fake_quantize.FakeQuantize TODO: special state_dict handling to include and exclude flags |
|
|
|
A simple STE backward function for numerical cast |
|
Drop-in replacement of torch.nn.quantized.DeQuantize that supports both torch.dtype and numerical.Format |
|
Mixin for modules with boundary casting |
|
Drop-in replacement of torch.nn.quantized.Quantize that supports both torch.dtype and numerical.Format |
- class dmx.compressor.numerical.cast.CastTo(format='SAME', observer=<class 'dmx.compressor.numerical.observer.DummyObserver'>, group_size=None, block_dim=-1, **fake_quantize_kwargs)
Bases:
FakeQuantizeSimulated numerical cast to a target format subclassing torch.quantization.fake_quantize.FakeQuantize TODO: special state_dict handling to include and exclude flags
- apply_shaping_seq(x, shaping_list)
- enable_calibration(state: bool = True, observer_cls: ~torch.ao.quantization.observer.ObserverBase = <class 'dmx.compressor.numerical.observer.HistogramObserver'>, qscheme_to_overload: ~torch.qscheme | None = None, group_size: int = None, ch_axis: int = None) None
- extra_repr()
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_precision() int | None
- set_pre_transform(pre_transform: Dict)
- class dmx.compressor.numerical.cast.CastToDict(modules: Mapping[str, Module] | None = None)
Bases:
ModuleDict- disable_fake_quant()
- disable_observer()
- enable_fake_quant()
- enable_observer()
- forward(x, *args, output=False, **kwargs)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- pack_to_dict(param)
- set_format(format: Dict | tuple | list)
- set_pre_transform(pre_transforms: Dict | tuple | list)
- class dmx.compressor.numerical.cast.CastToFormat(*args, **kwargs)
Bases:
FunctionA simple STE backward function for numerical cast
- static backward(ctx, g)
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjpfunction.)It must accept a context
ctxas the first argument, followed by as many outputs as theforward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,backward()will havectx.needs_input_grad[0] = Trueif the first input toforward()needs gradient computed w.r.t. the output.
- static forward(ctx, x, fmt, block_dim)
Define the forward of the custom autograd Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()staticmethod to handle setting up thectxobject.outputis the output of the forward,inputsare a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()if they are intended to be used inbackward(equivalently,vjp) orctx.save_for_forward()if they are intended to be used for injvp.
- static symbolic(g: Graph, input: Value, fmt: Value) Value
- class dmx.compressor.numerical.cast.DeQuantize(scale=None, zero_point=None, dtype=None)
Bases:
DeQuantizeDrop-in replacement of torch.nn.quantized.DeQuantize that supports both torch.dtype and numerical.Format
- Parameters:
scale – scale of the output Quantized Tensor
zero_point – zero_point of output Quantized Tensor
dtype – data type of output Quantized Tensor, either torch.dtype or Format
- `scale`, `zero_point`, `dtype`
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class dmx.compressor.numerical.cast.NumericalCastMixin(*args, **kwargs)
Bases:
objectMixin for modules with boundary casting
- property accum_format
- property bias_format
- check_format_dim_consistency() bool
- check_input_format_dim_consistency() bool
- check_residual_format_dim_consistency() bool
- check_weight_format_dim_consistency() bool
- infer_ch_axis()
- init_casts() None
- init_smoothquant(migration_strength: float = 0.5, scale_format: str | Format = 'SAME', dynamic: bool = False) None
- property input_formats
- property input_precision
- property multiplier_format
- property output_formats
- property residual_format
- property weight_format
- property weight_precision
- property weight_scale
- property weight_storage_format
- property weight_storage_precision
- property weight_storage_scale
- property weight_storage_zero_point
- property weight_zero_point
- class dmx.compressor.numerical.cast.Quantize(scale, zero_point, dtype: dtype | Format)
Bases:
QuantizeDrop-in replacement of torch.nn.quantized.Quantize that supports both torch.dtype and numerical.Format
- Parameters:
scale – scale of the output Quantized Tensor
zero_point – zero_point of output Quantized Tensor
dtype – data type of output Quantized Tensor, either torch.dtype or Format
- `scale`, `zero_point`, `dtype`
- forward(x)
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.