dmx.compressor.sparse
Classes
|
Bernoulli sampler for supermasking. |
|
Fine-grain structured sparsity with K non-zeros out of block_size elements long block_dim. |
|
This is a dummy sparsity whose forward and backward are equivalent to an identity function. |
|
|
|
This class inherits from torch.autograd.Function, since when we use masking operations, we may have different forward and backward passes. |
|
This is a sparsification management class Similar to Scheduler that manages parameter optimization through scheduler.step(), one can call sparsification_manager.step() to update underlying score. |
|
Sparsification module |
|
Fine-grain unstructured sparsity with top-K scored entries non-zero |
|
Mixin for weight-sparse modules. |
- class dmx.compressor.sparse.Bernoulli(mask_gradient=False)
Bases:
SparsenessBernoulli sampler for supermasking.
- static backward(ctx, grad_output)
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.
- blocked: bool = False
- static forward(ctx, score, params)
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.
- classmethod from_shorthand(sh: str)
- get_mask(score)
- class dmx.compressor.sparse.BlockTopK(K=4, block_size=8, block_dim=-1, mask_gradient=False)
Bases:
SparsenessFine-grain structured sparsity with K non-zeros out of block_size elements long block_dim.
- static backward(ctx, grad_output)
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.
- blocked: bool = True
- static forward(ctx, score, params)
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.
- classmethod from_shorthand(sh: str)
- get_mask(score)
- class dmx.compressor.sparse.Dense(mask_gradient=False)
Bases:
SparsenessThis is a dummy sparsity whose forward and backward are equivalent to an identity function.
- blocked: bool = False
- classmethod from_shorthand(sh: str)
- get_mask(score)
- class dmx.compressor.sparse.LazySparsify(sparseness='DENSE', backward_mode='STE', score_func=None)
Bases:
LazyModuleMixin,Sparsify- initialize_parameters(x: Tensor) None
Initialize parameters according to the input batch properties.
This adds an interface to isolate parameter initialization from the forward pass when doing parameter shape inference.
- reset_parameters() None
- score: UninitializedParameter
- class dmx.compressor.sparse.Sparseness(mask_gradient=False)
Bases:
FunctionThis class inherits from torch.autograd.Function, since when we use masking operations, we may have different forward and backward passes. Child classes to implement get_mask() and from_shorthand() method.
- static backward(ctx, grad_output)
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.
- blocked: bool
- classmethod from_shorthand(sh: str)
- get_mask(*input: Any)
- class dmx.compressor.sparse.SparsificationManager(sparsify_modules, **kwargs)
Bases:
objectThis is a sparsification management class Similar to Scheduler that manages parameter optimization through scheduler.step(), one can call sparsification_manager.step() to update underlying score.
- step(**kwargs)
- class dmx.compressor.sparse.Sparsify(tensor_shape, sparseness='DENSE', backward_mode='STE', score_func=None)
Bases:
ModuleSparsification module
- configure(sparseness=None, backward_mode=None, score_func=None)
- property density: float
- 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.
- mask_str(dims, max_elems)
- update_mask(score)
- class dmx.compressor.sparse.TopK(density=0.5, mask_gradient=False)
Bases:
SparsenessFine-grain unstructured sparsity with top-K scored entries non-zero
- blocked: bool = False
- static forward(ctx, score, params)
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.
- classmethod from_shorthand(sh: str)
- get_mask(score)