How to Add a New contrib Algorithm#
This page is the algorithm-specific companion to Quark’s contrib Area. It describes the mechanism
Quark provides for contributing a quantization algorithm for the PyTorch backend:
QuarkAlgorithm. Everything in Quark’s contrib Area —
the contribution principles, ownership, testing and legal requirements — still applies; this page
only covers the code you write.
Note
The current scope at the time of writing this is to add a developer friendly and safe way to stage new algorithms to quark that can be optionally used. Below is a detailed general example of how one may add an algorithm to quark using this system. In the future, once algorithms are implemented using this system, a more concrete example will be provided in this document.
Why QuarkAlgorithm#
Historically, adding a quantization algorithm to Quark meant editing three core files:
quark/torch/quantization/config/config.py— a branch in_load_quant_algo_config_from_dictso the algorithm’s JSON is parsed into a config object.quark/torch/quantization/config/algo_configs.py— anALGORITHM_CONFIG_MAPSentry holding the per-model-architecture default settings.quark/torch/algorithm/api.py— aPROCESSOR_MAPentry so the algorithm is dispatched.
Three files owned by the Quark Core Team, edited for every algorithm, by every author.
QuarkAlgorithm is a wrapper that bundles exactly those three contributions into a single
object, so your algorithm is a file you own plus one registration call — and no core file changes.
Anatomy of an Algorithm#
A QuarkAlgorithm has four fields:
Field |
What it is |
|---|---|
|
The algorithm name as it appears in |
|
The config dataclass your algorithm’s JSON is deserialized into. Replaces a branch of
|
|
The processor class that runs your algorithm. Replaces a |
|
Optional per-model-architecture default settings, keyed by model type ( |
Checklist#
Config class subclasses
AlgoConfigorPreQuantOptConfig, with a default for every field.Processor subclasses
BaseAlgoProcessor.Per-model defaults, if any, build a fresh config object per model type.
Algorithm passed to
ALGORITHM_REGISTRY.register, under a name no other algorithm uses, from a module that is imported before the algorithm is looked up.Tests,
README.md,CODEOWNERSentry and documentation as required by Quark’s contrib Area.
Using Your Algorithm#
Once registered, your algorithm behaves exactly like a built-in algorithm, and Quark uses it in the same way:
{"name": "myalgo", ...}in a config file deserializes toMyAlgoConfig.get_supported_algorithm_types()lists"myalgo", andget_algo_config("myalgo", "llama")returns your per-model default.get_processor("myalgo")dispatches toMyAlgoProcessor.
from quark.torch.quantization.config.algo_configs import get_algo_config
algo_config = get_algo_config("myalgo", "llama")
Tests and Documentation#
The requirements in Testing Your Contribution and Documenting Your Contribution apply unchanged. For a contributed algorithm specifically, please cover at least:
Your config class deserializes from a representative config dict, including any migration your
build_configperforms.get_processorreturns your processor, andget_algo_configyour per-model defaults, for your algorithm’s name.Each entry of your
algo_config_mapis an independent object.
Quark’s own tests live in test/test_for_torch/test_algo_registry.py and cover the registry
machinery itself; your tests belong in quark/contrib/your-component-name/tests/ and should
cover your algorithm.