diff --git a/docs/source/sparsification/flow-sparsification-model_recipe_sparsezoo-sparseml_transfer_learning.svg b/docs/source/sparsification/flow-sparsification-model_recipe_sparsezoo-sparseml_transfer_learning.svg new file mode 100644 index 00000000000..2381378d5f5 --- /dev/null +++ b/docs/source/sparsification/flow-sparsification-model_recipe_sparsezoo-sparseml_transfer_learning.svg @@ -0,0 +1,255 @@ + + \ No newline at end of file diff --git a/sparseml/_modules/index.html b/sparseml/_modules/index.html index e49298be4f4..234feb41f55 100644 --- a/sparseml/_modules/index.html +++ b/sparseml/_modules/index.html @@ -98,16 +98,17 @@
- + diff --git a/sparseml/_modules/sparseml/keras/optim/manager.html b/sparseml/_modules/sparseml/keras/optim/manager.html index a01a65e4944..a826c7ddfe5 100644 --- a/sparseml/_modules/sparseml/keras/optim/manager.html +++ b/sparseml/_modules/sparseml/keras/optim/manager.html @@ -98,16 +98,17 @@ - + @@ -202,6 +203,7 @@from sparseml.keras.utils.logger import KerasLogger
from sparseml.optim import BaseManager
from sparseml.utils import load_recipe_yaml_str
+from sparsezoo.objects import OptimizationRecipe
__all__ = ["ScheduledModifierManager"]
@@ -213,15 +215,23 @@ Source code for sparseml.keras.optim.manager
"""
[docs] @staticmethod
- def from_yaml(file_path: str, add_modifiers: List[Modifier] = None):
+ def from_yaml(
+ file_path: Union[str, OptimizationRecipe],
+ add_modifiers: List[Modifier] = None,
+ ):
"""
- Convenience function used to create the manager of multiple modifiers
- from a yaml file.
-
- :param file_path: the path to the yaml file to load the modifier from
+ Convenience function used to create the manager of multiple modifiers from a
+ recipe file.
+
+ :param file_path: the path to the recipe file to load the modifier from, or
+ a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+ SparseZoo stubs should be preceded by 'zoo:', and can contain an optional
+ '?recipe_type=<type>' parameter. Can also be a SparseZoo OptimizationRecipe
+ object. i.e. '/path/to/local/recipe.yaml', 'zoo:model/stub/path',
+ 'zoo:model/stub/path?recipe_type=transfer'
:param add_modifiers: additional modifiers that should be added to the
- returned manager alongside the ones loaded from the yaml file
- :return: ScheduledModifierManager() created from the yaml file
+ returned manager alongside the ones loaded from the recipe file
+ :return: ScheduledModifierManager() created from the recipe file
"""
yaml_str = load_recipe_yaml_str(file_path)
modifiers = Modifier.load_list(yaml_str)
diff --git a/sparseml/_modules/sparseml/keras/optim/mask_pruning.html b/sparseml/_modules/sparseml/keras/optim/mask_pruning.html
index 9d412a9ab74..c944425cb77 100644
--- a/sparseml/_modules/sparseml/keras/optim/mask_pruning.html
+++ b/sparseml/_modules/sparseml/keras/optim/mask_pruning.html
@@ -98,16 +98,17 @@
-
+
@@ -191,14 +192,21 @@ Source code for sparseml.keras.optim.mask_pruning
import abc
import collections
import inspect
-from typing import List
+from typing import List, Union
import tensorflow as tf
-from sparseml.keras.optim.mask_pruning_creator import PruningMaskCreator
+from sparseml.keras.optim.mask_pruning_creator import (
+ PruningMaskCreator,
+ load_mask_creator,
+)
-__all__ = ["MaskedLayer", "PruningScheduler", "remove_pruning_masks"]
+__all__ = [
+ "MaskedLayer",
+ "PruningScheduler",
+ "remove_pruning_masks",
+]
[docs]class PruningScheduler(abc.ABC):
@@ -206,6 +214,12 @@ Source code for sparseml.keras.optim.mask_pruning
Abstract pruning scheduler
"""
+ _REGISTRY = {}
+
+ def __init_subclass__(cls):
+ super().__init_subclass__()
+ PruningScheduler._register_class(cls)
+
[docs] @abc.abstractmethod
def should_prune(self, step: int) -> bool:
"""
@@ -225,7 +239,33 @@ Source code for sparseml.keras.optim.mask_pruning
:param kwargs: optional keyword params that a specific scheduler might need
:return: target sparsity
"""
- raise NotImplementedError("Not implemented")
+ raise NotImplementedError("Not implemented")
+
+
+
+[docs] @classmethod
+ def deserialize(cls, config):
+ """
+ Deserialize a pruning scheduler from config returned by scheduler's
+ get_config method
+
+ :param config: a pruning scheduler's config
+ :return: a pruning scheduler instance
+ """
+ if "class_name" not in config:
+ raise ValueError("The 'class_name' not found in config: {}".format(config))
+ class_name = config["class_name"]
+ return tf.keras.utils.deserialize_keras_object(
+ config,
+ module_objects=globals(),
+ custom_objects={class_name: PruningScheduler._REGISTRY[class_name]},
+ )
+
+ @classmethod
+ def _register_class(cls, target_cls):
+ PruningScheduler._REGISTRY[target_cls.__name__] = target_cls
MaskedParamInfo = collections.namedtuple(
@@ -368,7 +408,7 @@ Source code for sparseml.keras.optim.mask_pruning
self,
layer: tf.keras.layers.Layer,
pruning_scheduler: PruningScheduler,
- mask_creator: PruningMaskCreator,
+ mask_type: Union[str, List[int]] = "unstructured",
**kwargs,
):
if not isinstance(layer, MaskedLayer) and not isinstance(
@@ -381,7 +421,16 @@ Source code for sparseml.keras.optim.mask_pruning
super(MaskedLayer, self).__init__(layer, **kwargs)
self._layer = layer
self._pruning_scheduler = pruning_scheduler
- self._mask_creator = mask_creator
+ self._mask_type = mask_type
+ self._mask_creator = None
+ self._pruning_vars = []
+ self._global_step = None
+ self._mask_updater = None
+
+[docs] def build(self, input_shape):
+ super(MaskedLayer, self).build(input_shape)
+ self._mask_creator = load_mask_creator(self._mask_type)
+ self._pruning_vars = self._reuse_or_create_pruning_vars()
self._global_step = self.add_weight(
"global_step",
shape=[],
@@ -389,13 +438,12 @@ Source code for sparseml.keras.optim.mask_pruning
dtype=tf.int64,
trainable=False,
)
- self._pruning_vars = self._reuse_or_create_pruning_vars()
self._mask_updater = MaskAndWeightUpdater(
self._pruning_vars,
self._pruning_scheduler,
self._mask_creator,
self._global_step,
- )
+ )
def _reuse_or_create_pruning_vars(
self,
@@ -452,6 +500,44 @@ Source code for sparseml.keras.optim.mask_pruning
else:
return self._layer.call(inputs)
+[docs] def get_config(self):
+ """
+ Get layer config
+ Serialization and deserialization should be done using
+ tf.keras.serialize/deserialize, which create and retrieve the "class_name"
+ field automatically.
+ The resulting config below therefore does not contain the field.
+ """
+ config = super(MaskedLayer, self).get_config()
+ if "layer" not in config:
+ raise RuntimeError("Expected 'layer' field not found in config")
+ config.update(
+ {
+ "pruning_scheduler": self._pruning_scheduler.get_config(),
+ "mask_type": self._mask_type,
+ }
+ )
+ return config
+
+[docs] @classmethod
+ def from_config(cls, config):
+ config = config.copy()
+ layer = tf.keras.layers.deserialize(
+ config.pop("layer"), custom_objects={"MaskedLayer": MaskedLayer}
+ )
+ if not isinstance(layer, MaskedLayer) and not isinstance(
+ layer, tf.keras.layers.Layer
+ ):
+ raise RuntimeError("Unexpected layer created from config")
+ pruning_scheduler = PruningScheduler.deserialize(
+ config.pop("pruning_scheduler")
+ )
+ if not isinstance(pruning_scheduler, PruningScheduler):
+ raise RuntimeError("Unexpected pruning scheduler type created from config")
+ mask_type = config.pop("mask_type")
+ masked_layer = MaskedLayer(layer, pruning_scheduler, mask_type, **config)
+ return masked_layer
+
[docs] def compute_output_shape(self, input_shape):
return self._layer.compute_output_shape(input_shape)
@@ -478,7 +564,11 @@ Source code for sparseml.keras.optim.mask_pruning
elif isinstance(self._layer, tf.keras.layers.Layer):
return self._layer
else:
- raise RuntimeError("Unrecognized layer")
+ raise RuntimeError("Unrecognized layer")
+
+ @property
+ def masked_layer(self):
+ return self._layer
[docs]def remove_pruning_masks(model: tf.keras.Model):
diff --git a/sparseml/_modules/sparseml/keras/optim/mask_pruning_creator.html b/sparseml/_modules/sparseml/keras/optim/mask_pruning_creator.html
index ba84cc5c6f4..e5305105cac 100644
--- a/sparseml/_modules/sparseml/keras/optim/mask_pruning_creator.html
+++ b/sparseml/_modules/sparseml/keras/optim/mask_pruning_creator.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/optim/modifier.html b/sparseml/_modules/sparseml/keras/optim/modifier.html
index ec4b8f040d0..cc6c1707433 100644
--- a/sparseml/_modules/sparseml/keras/optim/modifier.html
+++ b/sparseml/_modules/sparseml/keras/optim/modifier.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/optim/modifier_epoch.html b/sparseml/_modules/sparseml/keras/optim/modifier_epoch.html
index 925a105650a..231786169ee 100644
--- a/sparseml/_modules/sparseml/keras/optim/modifier_epoch.html
+++ b/sparseml/_modules/sparseml/keras/optim/modifier_epoch.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/optim/modifier_lr.html b/sparseml/_modules/sparseml/keras/optim/modifier_lr.html
index 8af0dc770c5..688997aae82 100644
--- a/sparseml/_modules/sparseml/keras/optim/modifier_lr.html
+++ b/sparseml/_modules/sparseml/keras/optim/modifier_lr.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/optim/modifier_params.html b/sparseml/_modules/sparseml/keras/optim/modifier_params.html
index 9eb292b30f0..f6817488c8f 100644
--- a/sparseml/_modules/sparseml/keras/optim/modifier_params.html
+++ b/sparseml/_modules/sparseml/keras/optim/modifier_params.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/optim/modifier_pruning.html b/sparseml/_modules/sparseml/keras/optim/modifier_pruning.html
index 250a2dff294..997014dfedd 100644
--- a/sparseml/_modules/sparseml/keras/optim/modifier_pruning.html
+++ b/sparseml/_modules/sparseml/keras/optim/modifier_pruning.html
@@ -98,16 +98,17 @@
-
+
@@ -202,10 +203,6 @@ Source code for sparseml.keras.optim.modifier_pruning
PruningScheduler,
remove_pruning_masks,
)
-from sparseml.keras.optim.mask_pruning_creator import (
- PruningMaskCreator,
- load_mask_creator,
-)
from sparseml.keras.optim.modifier import (
KerasModifierYAML,
ModifierProp,
@@ -249,6 +246,14 @@ Source code for sparseml.keras.optim.modifier_pruning
self._update_frequency_steps = update_frequency_steps
self._inter_func = inter_func
+ @property
+ def init_sparsity(self):
+ return self._init_sparsity
+
+ @property
+ def final_sparsity(self):
+ return self._final_sparsity
+
@property
def start_step(self):
return self._start_step
@@ -261,6 +266,10 @@ Source code for sparseml.keras.optim.modifier_pruning
def update_frequency_steps(self):
return self._update_frequency_steps
+ @property
+ def inter_func(self):
+ return self._inter_func
+
@property
def exponent(self) -> float:
"""
@@ -330,6 +339,20 @@ Source code for sparseml.keras.optim.modifier_pruning
sparsity = self._final_sparsity
return sparsity
+ def get_config(self):
+ config = {
+ "class_name": self.__class__.__name__,
+ "config": {
+ "init_sparsity": self.init_sparsity,
+ "final_sparsity": self.final_sparsity,
+ "start_step": self.start_step,
+ "end_step": self.end_step,
+ "update_frequency_steps": self.update_frequency_steps,
+ "inter_func": self.inter_func,
+ },
+ }
+ return config
+
class SparsityFreezer(PruningScheduler):
"""
@@ -348,6 +371,14 @@ Source code for sparseml.keras.optim.modifier_pruning
self._start_step = start_step
self._end_step = end_step
+ @property
+ def start_step(self):
+ return self._start_step
+
+ @property
+ def end_step(self):
+ return self._ends_step
+
def should_prune(self, step: int) -> bool:
"""
Check if the given step is a right time for pruning
@@ -379,6 +410,14 @@ Source code for sparseml.keras.optim.modifier_pruning
sparsity = None
return sparsity
+ def get_config(self):
+ config = {
+ "class_name": self.__class__.__name__,
+ "start_step": self.start_step,
+ "end_step": self.end_step,
+ }
+ return config
+
class PruningModifierCallback(tensorflow.keras.callbacks.Callback):
"""
@@ -521,7 +560,7 @@ Source code for sparseml.keras.optim.modifier_pruning
[docs]@KerasModifierYAML()
-class ConstantPruningModifier(ScheduledModifier, PruningScheduler):
+class ConstantPruningModifier(ScheduledModifier):
"""
Holds the sparsity level and shape for a given param constant while training.
Useful for transfer learning use cases.
@@ -563,7 +602,7 @@ Source code for sparseml.keras.optim.modifier_pruning
self._masked_layers = []
self._sparsity_scheduler = None
- self._mask_creator = load_mask_creator("unstructured")
+ self._mask_type = "unstructured"
@ModifierProp()
def params(self) -> Union[str, List[str]]:
@@ -632,7 +671,7 @@ Source code for sparseml.keras.optim.modifier_pruning
cloned_layer = layer
if layer.name in self.layer_names: # TODO: handle regex params
cloned_layer = MaskedLayer(
- layer, self._sparsity_scheduler, self._mask_creator, name=layer.name
+ layer, self._sparsity_scheduler, self._mask_type, name=layer.name
)
self._masked_layers.append(cloned_layer)
return cloned_layer
@@ -729,7 +768,7 @@ Source code for sparseml.keras.optim.modifier_pruning
default is __ALL__
:param mask_type: String to define type of sparsity (options: ['unstructured',
'channel', 'filter']), List to define block shape of a parameter's in and out
- channels, or a PruningMaskCreator object. default is 'unstructured'
+ channels. default is 'unstructured'
:param leave_enabled: True to continue masking the weights after end_epoch,
False to stop masking. Should be set to False if exporting the result
immediately after or doing some other prune
@@ -745,7 +784,7 @@ Source code for sparseml.keras.optim.modifier_pruning
update_frequency: float,
inter_func: str = "cubic",
log_types: Union[str, List[str]] = ALL_TOKEN,
- mask_type: Union[str, List[int], PruningMaskCreator] = "unstructured",
+ mask_type: Union[str, List[int]] = "unstructured",
leave_enabled: bool = True,
):
super(GMPruningModifier, self).__init__(
@@ -767,10 +806,7 @@ Source code for sparseml.keras.optim.modifier_pruning
self._leave_enabled = convert_to_bool(leave_enabled)
self._inter_func = inter_func
self._mask_type = mask_type
- self._mask_creator = mask_type
self._leave_enabled = convert_to_bool(leave_enabled)
- if not isinstance(mask_type, PruningMaskCreator):
- self._mask_creator = load_mask_creator(mask_type)
self._prune_op_vars = None
self._update_ready = None
self._sparsity = None
@@ -870,21 +906,18 @@ Source code for sparseml.keras.optim.modifier_pruning
self.validate()
@ModifierProp()
- def mask_type(self) -> Union[str, List[int], PruningMaskCreator]:
+ def mask_type(self) -> Union[str, List[int]]:
"""
- :return: the PruningMaskCreator object used
+ :return: the mask type used
"""
return self._mask_type
@mask_type.setter
- def mask_type(self, value: Union[str, List[int], PruningMaskCreator]):
+ def mask_type(self, value: Union[str, List[int]]):
"""
- :param value: the PruningMaskCreator object to use
+ :param value: the mask type to use
"""
self._mask_type = value
- self._mask_creator = value
- if not isinstance(value, PruningMaskCreator):
- self._mask_creator = load_mask_creator(value)
@ModifierProp()
def leave_enabled(self) -> bool:
@@ -1010,7 +1043,7 @@ Source code for sparseml.keras.optim.modifier_pruning
layer.name in self.layer_names
): # TODO: handle regex params --- see create_ops in TF version
cloned_layer = MaskedLayer(
- layer, self._sparsity_scheduler, self._mask_creator, name=layer.name
+ layer, self._sparsity_scheduler, self._mask_type, name=layer.name
)
self._masked_layers.append(cloned_layer)
return cloned_layer
diff --git a/sparseml/_modules/sparseml/keras/optim/utils.html b/sparseml/_modules/sparseml/keras/optim/utils.html
index f1b24b7c4c0..b461df22a29 100644
--- a/sparseml/_modules/sparseml/keras/optim/utils.html
+++ b/sparseml/_modules/sparseml/keras/optim/utils.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/utils/callbacks.html b/sparseml/_modules/sparseml/keras/utils/callbacks.html
index c06db171900..700944f89eb 100644
--- a/sparseml/_modules/sparseml/keras/utils/callbacks.html
+++ b/sparseml/_modules/sparseml/keras/utils/callbacks.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/utils/exporter.html b/sparseml/_modules/sparseml/keras/utils/exporter.html
index dd4e44409b6..22c38071af1 100644
--- a/sparseml/_modules/sparseml/keras/utils/exporter.html
+++ b/sparseml/_modules/sparseml/keras/utils/exporter.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/utils/logger.html b/sparseml/_modules/sparseml/keras/utils/logger.html
index bc97f7f462e..5f2e472691f 100644
--- a/sparseml/_modules/sparseml/keras/utils/logger.html
+++ b/sparseml/_modules/sparseml/keras/utils/logger.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/keras/utils/model.html b/sparseml/_modules/sparseml/keras/utils/model.html
index a1f014dff27..d1039156644 100644
--- a/sparseml/_modules/sparseml/keras/utils/model.html
+++ b/sparseml/_modules/sparseml/keras/utils/model.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/log.html b/sparseml/_modules/sparseml/log.html
index d5ddd319439..277d3c40ad0 100644
--- a/sparseml/_modules/sparseml/log.html
+++ b/sparseml/_modules/sparseml/log.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/optim/analyzer_model.html b/sparseml/_modules/sparseml/onnx/optim/analyzer_model.html
index f7035036c57..6845ab6a638 100644
--- a/sparseml/_modules/sparseml/onnx/optim/analyzer_model.html
+++ b/sparseml/_modules/sparseml/onnx/optim/analyzer_model.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/optim/quantization/calibration.html b/sparseml/_modules/sparseml/onnx/optim/quantization/calibration.html
index ce688a96c53..a5befd5d188 100644
--- a/sparseml/_modules/sparseml/onnx/optim/quantization/calibration.html
+++ b/sparseml/_modules/sparseml/onnx/optim/quantization/calibration.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/optim/quantization/quantize.html b/sparseml/_modules/sparseml/onnx/optim/quantization/quantize.html
index 6f34a38be56..ecf9425ea3e 100644
--- a/sparseml/_modules/sparseml/onnx/optim/quantization/quantize.html
+++ b/sparseml/_modules/sparseml/onnx/optim/quantization/quantize.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/optim/quantization/quantize_model_post_training.html b/sparseml/_modules/sparseml/onnx/optim/quantization/quantize_model_post_training.html
index b4e79bf4dcc..248f3d32ab9 100644
--- a/sparseml/_modules/sparseml/onnx/optim/quantization/quantize_model_post_training.html
+++ b/sparseml/_modules/sparseml/onnx/optim/quantization/quantize_model_post_training.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/optim/sensitivity_pruning.html b/sparseml/_modules/sparseml/onnx/optim/sensitivity_pruning.html
index 64eec13f9c5..25b34d88e41 100644
--- a/sparseml/_modules/sparseml/onnx/optim/sensitivity_pruning.html
+++ b/sparseml/_modules/sparseml/onnx/optim/sensitivity_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/data.html b/sparseml/_modules/sparseml/onnx/utils/data.html
index 8468468b41a..63f67979f34 100644
--- a/sparseml/_modules/sparseml/onnx/utils/data.html
+++ b/sparseml/_modules/sparseml/onnx/utils/data.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/graph_editor.html b/sparseml/_modules/sparseml/onnx/utils/graph_editor.html
index 694ee85eb2d..f3c9ae4a055 100644
--- a/sparseml/_modules/sparseml/onnx/utils/graph_editor.html
+++ b/sparseml/_modules/sparseml/onnx/utils/graph_editor.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/graph_optimizer.html b/sparseml/_modules/sparseml/onnx/utils/graph_optimizer.html
index 5ae53dfece3..a4251a3b28a 100644
--- a/sparseml/_modules/sparseml/onnx/utils/graph_optimizer.html
+++ b/sparseml/_modules/sparseml/onnx/utils/graph_optimizer.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/helpers.html b/sparseml/_modules/sparseml/onnx/utils/helpers.html
index 846040390cc..94b9237e393 100644
--- a/sparseml/_modules/sparseml/onnx/utils/helpers.html
+++ b/sparseml/_modules/sparseml/onnx/utils/helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/loss.html b/sparseml/_modules/sparseml/onnx/utils/loss.html
index 65aa26bd840..2ab7ca8eb25 100644
--- a/sparseml/_modules/sparseml/onnx/utils/loss.html
+++ b/sparseml/_modules/sparseml/onnx/utils/loss.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/model.html b/sparseml/_modules/sparseml/onnx/utils/model.html
index 174a9b8b066..282e20a9603 100644
--- a/sparseml/_modules/sparseml/onnx/utils/model.html
+++ b/sparseml/_modules/sparseml/onnx/utils/model.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/onnx/utils/sparse_tensor.html b/sparseml/_modules/sparseml/onnx/utils/sparse_tensor.html
index cb117c3e8de..758c4edd994 100644
--- a/sparseml/_modules/sparseml/onnx/utils/sparse_tensor.html
+++ b/sparseml/_modules/sparseml/onnx/utils/sparse_tensor.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/optim/analyzer.html b/sparseml/_modules/sparseml/optim/analyzer.html
index ec03b855af0..6ad0db49ccb 100644
--- a/sparseml/_modules/sparseml/optim/analyzer.html
+++ b/sparseml/_modules/sparseml/optim/analyzer.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/optim/learning_rate.html b/sparseml/_modules/sparseml/optim/learning_rate.html
index 35cb8ee6c5a..a87eecff556 100644
--- a/sparseml/_modules/sparseml/optim/learning_rate.html
+++ b/sparseml/_modules/sparseml/optim/learning_rate.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/optim/manager.html b/sparseml/_modules/sparseml/optim/manager.html
index 857d89003a3..6037c8a4e61 100644
--- a/sparseml/_modules/sparseml/optim/manager.html
+++ b/sparseml/_modules/sparseml/optim/manager.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/optim/modifier.html b/sparseml/_modules/sparseml/optim/modifier.html
index c02b192ea96..7eb93702d1f 100644
--- a/sparseml/_modules/sparseml/optim/modifier.html
+++ b/sparseml/_modules/sparseml/optim/modifier.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/optim/sensitivity.html b/sparseml/_modules/sparseml/optim/sensitivity.html
index fa202af4aa2..f039fedad85 100644
--- a/sparseml/_modules/sparseml/optim/sensitivity.html
+++ b/sparseml/_modules/sparseml/optim/sensitivity.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/classification/cifar.html b/sparseml/_modules/sparseml/pytorch/datasets/classification/cifar.html
index ea60e60e725..5c529048104 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/classification/cifar.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/classification/cifar.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagefolder.html b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagefolder.html
index 7704190c1cc..fa83de71554 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagefolder.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagefolder.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenet.html b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenet.html
index 852b95fd48e..49f1be61b65 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenet.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenette.html b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenette.html
index 0c1fd7ed666..4deb918ec3b 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenette.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/classification/imagenette.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/classification/mnist.html b/sparseml/_modules/sparseml/pytorch/datasets/classification/mnist.html
index ee8c32c89ba..dc48d3b372a 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/classification/mnist.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/classification/mnist.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/detection/coco.html b/sparseml/_modules/sparseml/pytorch/datasets/detection/coco.html
index 5a9bb1efc06..25f201479a4 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/detection/coco.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/detection/coco.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/detection/helpers.html b/sparseml/_modules/sparseml/pytorch/datasets/detection/helpers.html
index fde48658893..76f826a5a90 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/detection/helpers.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/detection/helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/detection/voc.html b/sparseml/_modules/sparseml/pytorch/datasets/detection/voc.html
index 498a858510e..2f53836eb67 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/detection/voc.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/detection/voc.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/generic.html b/sparseml/_modules/sparseml/pytorch/datasets/generic.html
index 22680a27947..5ec36702dc5 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/generic.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/generic.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/datasets/registry.html b/sparseml/_modules/sparseml/pytorch/datasets/registry.html
index 489afb94c4f..ea6d613ee85 100644
--- a/sparseml/_modules/sparseml/pytorch/datasets/registry.html
+++ b/sparseml/_modules/sparseml/pytorch/datasets/registry.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/darknet.html b/sparseml/_modules/sparseml/pytorch/models/classification/darknet.html
index e1ab78a8ae6..76d66affbef 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/darknet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/darknet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/efficientnet.html b/sparseml/_modules/sparseml/pytorch/models/classification/efficientnet.html
index 91572557868..a79f6062a9f 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/efficientnet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/efficientnet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/inception_v3.html b/sparseml/_modules/sparseml/pytorch/models/classification/inception_v3.html
index 20ee94e64ce..d8ed1ea6277 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/inception_v3.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/inception_v3.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/mnist.html b/sparseml/_modules/sparseml/pytorch/models/classification/mnist.html
index b079dbc2df0..8e6bf7fb71e 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/mnist.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/mnist.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet.html b/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet.html
index 2ac81ae5e29..7e021c4f757 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet_v2.html b/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet_v2.html
index 84f2a70f89d..46373c71618 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet_v2.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/mobilenet_v2.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/resnet.html b/sparseml/_modules/sparseml/pytorch/models/classification/resnet.html
index ac62b4e6724..4ee6f25a162 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/resnet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/resnet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/classification/vgg.html b/sparseml/_modules/sparseml/pytorch/models/classification/vgg.html
index 48e63945e4a..d4dee421eff 100644
--- a/sparseml/_modules/sparseml/pytorch/models/classification/vgg.html
+++ b/sparseml/_modules/sparseml/pytorch/models/classification/vgg.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/detection/ssd.html b/sparseml/_modules/sparseml/pytorch/models/detection/ssd.html
index 3b5e907dec4..e78f0363373 100644
--- a/sparseml/_modules/sparseml/pytorch/models/detection/ssd.html
+++ b/sparseml/_modules/sparseml/pytorch/models/detection/ssd.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_lite.html b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_lite.html
index 008f05b7d29..9654b86b7ee 100644
--- a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_lite.html
+++ b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_lite.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_mobilenet.html b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_mobilenet.html
index 90fa9db8c8b..f6d947277b1 100644
--- a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_mobilenet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_mobilenet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_resnet.html b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_resnet.html
index 0a5dfbbddf9..cf0d7ed883a 100644
--- a/sparseml/_modules/sparseml/pytorch/models/detection/ssd_resnet.html
+++ b/sparseml/_modules/sparseml/pytorch/models/detection/ssd_resnet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/detection/yolo_v3.html b/sparseml/_modules/sparseml/pytorch/models/detection/yolo_v3.html
index ba94229718e..8d3b4981a5b 100644
--- a/sparseml/_modules/sparseml/pytorch/models/detection/yolo_v3.html
+++ b/sparseml/_modules/sparseml/pytorch/models/detection/yolo_v3.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/models/registry.html b/sparseml/_modules/sparseml/pytorch/models/registry.html
index 5f4ffb3b5fa..ae7688b86bc 100644
--- a/sparseml/_modules/sparseml/pytorch/models/registry.html
+++ b/sparseml/_modules/sparseml/pytorch/models/registry.html
@@ -98,16 +98,17 @@
-
+
@@ -484,7 +485,10 @@ Source code for sparseml.pytorch.models.registry
):
"""
:param pretrained_path: A path to the pretrained weights to load,
- if provided will override the pretrained param
+ if provided will override the pretrained param. May also be
+ a SparseZoo stub path preceded by 'zoo:' with the optional
+ `?recipe_type=` argument. If given a recipe type, the base
+ model weights for that recipe will be loaded
:param pretrained: True to load the default pretrained weights,
a string to load a specific pretrained weight
(ex: base, optim, optim-perf),
diff --git a/sparseml/_modules/sparseml/pytorch/nn/activations.html b/sparseml/_modules/sparseml/pytorch/nn/activations.html
index 80cfbc572d9..b1af7ca3b29 100644
--- a/sparseml/_modules/sparseml/pytorch/nn/activations.html
+++ b/sparseml/_modules/sparseml/pytorch/nn/activations.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/nn/fatrelu.html b/sparseml/_modules/sparseml/pytorch/nn/fatrelu.html
index f1b41894de3..50ce0eec033 100644
--- a/sparseml/_modules/sparseml/pytorch/nn/fatrelu.html
+++ b/sparseml/_modules/sparseml/pytorch/nn/fatrelu.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/nn/se.html b/sparseml/_modules/sparseml/pytorch/nn/se.html
index 9890fe027a8..fcc22a83c7d 100644
--- a/sparseml/_modules/sparseml/pytorch/nn/se.html
+++ b/sparseml/_modules/sparseml/pytorch/nn/se.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/analyzer_as.html b/sparseml/_modules/sparseml/pytorch/optim/analyzer_as.html
index 0ae7677eb16..98f569e354c 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/analyzer_as.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/analyzer_as.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/analyzer_module.html b/sparseml/_modules/sparseml/pytorch/optim/analyzer_module.html
index 3820366d171..13e93675254 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/analyzer_module.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/analyzer_module.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/analyzer_pruning.html b/sparseml/_modules/sparseml/pytorch/optim/analyzer_pruning.html
index e996fbe983e..babdfb2be13 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/analyzer_pruning.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/analyzer_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/manager.html b/sparseml/_modules/sparseml/pytorch/optim/manager.html
index 3d5241a7c03..26e11f6f8d3 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/manager.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/manager.html
@@ -98,16 +98,17 @@
-
+
@@ -205,6 +206,7 @@ Source code for sparseml.pytorch.optim.manager
from sparseml.pytorch.optim.modifier import Modifier, ScheduledModifier
from sparseml.pytorch.utils import PyTorchLogger
from sparseml.utils import load_recipe_yaml_str
+from sparsezoo.objects import OptimizationRecipe
__all__ = ["ScheduledModifierManager", "load_manager"]
@@ -232,15 +234,23 @@
Source code for sparseml.pytorch.optim.manager
"""
[docs] @staticmethod
- def from_yaml(file_path: str, add_modifiers: List[Modifier] = None):
+ def from_yaml(
+ file_path: Union[str, OptimizationRecipe],
+ add_modifiers: List[Modifier] = None,
+ ):
"""
Convenience function used to create the manager of multiple modifiers from a
- yaml file.
-
- :param file_path: the path to the yaml file to load the modifier from
+ recipe file.
+
+ :param file_path: the path to the recipe file to load the modifier from, or
+ a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+ SparseZoo stubs should be preceded by 'zoo:', and can contain an optional
+ '?recipe_type=<type>' parameter. Can also be a SparseZoo OptimizationRecipe
+ object. i.e. '/path/to/local/recipe.yaml', 'zoo:model/stub/path',
+ 'zoo:model/stub/path?recipe_type=transfer'
:param add_modifiers: additional modifiers that should be added to the
- returned manager alongside the ones loaded from the yaml file
- :return: ScheduledModifierManager() created from the yaml file
+ returned manager alongside the ones loaded from the recipe file
+ :return: ScheduledModifierManager() created from the recipe file
"""
yaml_str = load_recipe_yaml_str(file_path)
modifiers = Modifier.load_list(yaml_str)
diff --git a/sparseml/_modules/sparseml/pytorch/optim/mask_creator_pruning.html b/sparseml/_modules/sparseml/pytorch/optim/mask_creator_pruning.html
index 60d35a9a1d2..0dd39ac1c04 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/mask_creator_pruning.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/mask_creator_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/mask_pruning.html b/sparseml/_modules/sparseml/pytorch/optim/mask_pruning.html
index 122dc76cb0e..d8ba570392b 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/mask_pruning.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/mask_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier.html b/sparseml/_modules/sparseml/pytorch/optim/modifier.html
index bce42ba1cad..875cd5dc3a0 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_as.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_as.html
index 377f2ec25a4..8dea0c1ec10 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_as.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_as.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_epoch.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_epoch.html
index 6c33d1fcfb6..a8ca4a5a230 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_epoch.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_epoch.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_lr.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_lr.html
index 8359bb99629..84538abbc4e 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_lr.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_lr.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_params.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_params.html
index cc054c8f7bd..1636a6bff4d 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_params.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_params.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_pruning.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_pruning.html
index 2b8e293816d..088721ebefb 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_pruning.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_pruning.html
@@ -98,16 +98,17 @@
-
+
@@ -253,7 +254,7 @@ Source code for sparseml.pytorch.optim.modifier_pruning
Useful for transfer learning use cases.
| Sample yaml:
- | !ConstantKSModifier
+ | !ConstantPruningModifier
| start_epoch: 0.0
| end_epoch: 10.0
| params: ['re:.*weight']
@@ -346,7 +347,7 @@ Source code for sparseml.pytorch.optim.modifier_pruning
if param_name not in module_masks:
raise RuntimeError(
f"Unexpected parameter name when loading state dict for "
- f"ConstantKSModifier Manager has parameters "
+ f"ConstantPruningModifier Manager has parameters "
f"{list(module_masks.keys())}, given {param_name}"
)
mask_disabled = False
@@ -480,7 +481,7 @@ Source code for sparseml.pytorch.optim.modifier_pruning
Applies based on magnitude pruning unless otherwise specified by mask_type.
| Sample yaml:
- | !GradualKSModifier
+ | !GMPruningModifier
| init_sparsity: 0.05
| final_sparsity: 0.8
| start_epoch: 0.0
@@ -588,7 +589,7 @@ Source code for sparseml.pytorch.optim.modifier_pruning
if param_name not in module_masks:
raise RuntimeError(
f"Unexpected parameter name when loading state dict for "
- f"GradualKSModifier Manager has parameters "
+ f"GMPruningModifier Manager has parameters "
f"{list(module_masks.keys())}, given {param_name}"
)
mask_disabled = False
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_quantization.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_quantization.html
index 45dd60e300e..66d42ca0642 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_quantization.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_quantization.html
@@ -98,16 +98,17 @@
-
+
@@ -195,7 +196,7 @@ Source code for sparseml.pytorch.optim.modifier_quantization
"""
-from typing import List, Union
+from typing import Any, Dict, List, Union
from torch.nn import Module
from torch.optim.optimizer import Optimizer
@@ -251,6 +252,8 @@ Source code for sparseml.pytorch.optim.modifier_quantization
None to not stop tracking batch norm stats during QAT. Default is None
:param end_epoch: Disabled, setting to anything other than -1 will raise an
exception. For compatibility with YAML serialization only.
+ :param model_fuse_fn_kwargs: dictionary of keyword argument values to be passed
+ to the model fusing function
"""
def __init__(
@@ -261,6 +264,7 @@ Source code for sparseml.pytorch.optim.modifier_quantization
disable_quantization_observer_epoch: Union[float, None] = None,
freeze_bn_stats_epoch: Union[float, None] = None,
end_epoch: float = -1,
+ model_fuse_fn_kwargs: Dict[str, Any] = None,
):
if torch_quantization is None or torch_intrinsic is None:
raise RuntimeError(
@@ -279,6 +283,7 @@ Source code for sparseml.pytorch.optim.modifier_quantization
self._start_epoch = start_epoch
self._submodules = submodules
self._model_fuse_fn_name = model_fuse_fn_name
+ self._model_fuse_fn_kwargs = model_fuse_fn_kwargs or {}
self._disable_quantization_observer_epoch = disable_quantization_observer_epoch
self._freeze_bn_stats_epoch = freeze_bn_stats_epoch
@@ -430,9 +435,10 @@ Source code for sparseml.pytorch.optim.modifier_quantization
self._model_fuse_fn_name
)
)
- module_fuse_fn()
+ module_fuse_fn(**self._model_fuse_fn_kwargs)
elif self._model_fuse_fn_name is None: # default auto fn
- fuse_module_conv_bn_relus(module, inplace=True)
+ self._model_fuse_fn_kwargs["inplace"] = True
+ fuse_module_conv_bn_relus(module, **self._model_fuse_fn_kwargs)
# prepare each module / submodule for quantization
qconfig = get_qat_qconfig()
for quant_module in self._modules_to_quantize:
diff --git a/sparseml/_modules/sparseml/pytorch/optim/modifier_regularizer.html b/sparseml/_modules/sparseml/pytorch/optim/modifier_regularizer.html
index 23fb4b3d48f..fc696fb020f 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/modifier_regularizer.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/modifier_regularizer.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/optimizer.html b/sparseml/_modules/sparseml/pytorch/optim/optimizer.html
index 0967f3b1f16..d71bb5983d1 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/optimizer.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/optimizer.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/quantization/helpers.html b/sparseml/_modules/sparseml/pytorch/optim/quantization/helpers.html
index 0fe4dc1e6db..9d2f07805b7 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/quantization/helpers.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/quantization/helpers.html
@@ -98,16 +98,17 @@
-
+
@@ -193,14 +194,17 @@ Source code for sparseml.pytorch.optim.quantization.helpers
"""
from copy import deepcopy
+from typing import Union
import torch
from torch.nn import BatchNorm2d, Conv2d, Module, ReLU
try:
+ import torch.nn.intrinsic as nni
from torch import quantization as torch_quantization
except Exception:
+ nni = None
torch_quantization = None
from sparseml.pytorch.nn import ReLU as ReLU_nm
@@ -213,15 +217,40 @@ Source code for sparseml.pytorch.optim.quantization.helpers
]
+_QUANTIZABLE_MODULE_TYPES = (
+ {
+ # Conv based layers
+ torch.nn.Conv1d,
+ torch.nn.Conv2d,
+ torch.nn.Conv3d,
+ nni.ConvBn1d,
+ nni.ConvBn2d,
+ nni.ConvBn3d,
+ nni.ConvReLU1d,
+ nni.ConvReLU2d,
+ nni.ConvReLU3d,
+ nni.ConvBnReLU1d,
+ nni.ConvBnReLU2d,
+ nni.ConvBnReLU3d,
+ # Linear Layers
+ torch.nn.Linear,
+ nni.LinearReLU,
+ }
+ if nni # nni will always import if torch.quantization is available
+ else None
+)
+
+
[docs]def add_quant_dequant(module):
"""
Wraps all Conv and Linear submodule with a qconfig with a QuantWrapper
:param module: the module to modify
"""
- module_type = str(type(module)).split(".")[-1].lower()
- is_quantizable_module = "conv" in module_type or "linear" in module_type
-
- if is_quantizable_module and hasattr(module, "qconfig") and module.qconfig:
+ if (
+ type(module) in _QUANTIZABLE_MODULE_TYPES
+ and hasattr(module, "qconfig")
+ and module.qconfig
+ ):
return torch_quantization.QuantWrapper(module)
for name, child in module.named_children():
@@ -252,7 +281,11 @@ Source code for sparseml.pytorch.optim.quantization.helpers
)
-[docs]def fuse_module_conv_bn_relus(module: Module, inplace: bool = True) -> Module:
+[docs]def fuse_module_conv_bn_relus(
+ module: Module,
+ inplace: bool = True,
+ override_bn_subclasses_forward: Union[bool, str] = True,
+) -> Module:
"""
Performs fusion of Conv2d, BatchNorm2d, and ReLU layers found in the
given module. To be fused, these layers must appear sequentially in
@@ -264,6 +297,12 @@ Source code for sparseml.pytorch.optim.quantization.helpers
:param module: the module to fuse
:param inplace: set True to perform fusions in-place. default is True
+ :param override_bn_subclasses_forward: if True, modules that are subclasses of
+ BatchNorm2d will be modified to be BatchNorm2d but with the forward
+ pass and state variables copied from the subclass. This is so these
+ BN modules can pass PyTorch type checking when fusing. Can set to
+ "override-only" and only parameters will be overwritten, not the
+ forward pass. Default is True
:return: the fused module
"""
if torch_quantization is None:
@@ -288,7 +327,22 @@ Source code for sparseml.pytorch.optim.quantization.helpers
and submodule_name == current_block_submodule_name
):
if isinstance(layer, ReLU_nm):
- _replace_nm_relu(module, name, layer)
+ _set_submodule(module, name, ReLU(inplace=layer.inplace))
+ if isinstance(layer, BatchNorm2d) and not type(layer) is BatchNorm2d:
+ if not override_bn_subclasses_forward:
+ raise RuntimeError(
+ "Detected a Conv-BN block that uses a subclass of BatchNorm2d. "
+ "This will cause a type error when fusing with PyTorch, "
+ "set override_bn_subclasses_forward to True or 'override-only "
+ "to modify this BN subclass to be a BatchNorm2d object"
+ )
+ # swap BN subclass with overwritten BN class that will pass torch
+ # type checking
+ overwritten_bn = _wrap_bn_sub_class(
+ layer,
+ override_forward=override_bn_subclasses_forward != "override-only",
+ )
+ _set_submodule(module, name, overwritten_bn),
current_block.append(name)
else:
if current_block:
@@ -304,13 +358,21 @@ Source code for sparseml.pytorch.optim.quantization.helpers
return module
-def _replace_nm_relu(root_module, relu_path, nm_relu):
+def _set_submodule(root_module, sub_module_path, sub_module):
current_module = root_module
- relu_path = relu_path.split(".")
- for sub_module in relu_path[:-1]:
- current_module = getattr(current_module, sub_module)
- new_relu = ReLU(inplace=nm_relu.inplace)
- setattr(current_module, relu_path[-1], new_relu)
+ sub_module_path = sub_module_path.split(".")
+ for child_module in sub_module_path[:-1]:
+ current_module = getattr(current_module, child_module)
+ setattr(current_module, sub_module_path[-1], sub_module)
+
+
+def _wrap_bn_sub_class(bn_subclass, override_forward=True):
+ batch_norm = BatchNorm2d(bn_subclass.num_features)
+ batch_norm.__dict__ = bn_subclass.__dict__
+ if override_forward:
+ batch_norm.forward = bn_subclass.forward
+ del bn_subclass
+ return batch_norm
diff --git a/sparseml/_modules/sparseml/pytorch/optim/quantization/quantize_qat_export.html b/sparseml/_modules/sparseml/pytorch/optim/quantization/quantize_qat_export.html
index d453bfcd42d..e0348a9d1aa 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/quantization/quantize_qat_export.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/quantization/quantize_qat_export.html
@@ -98,16 +98,17 @@
-
+
@@ -745,15 +746,23 @@ Source code for sparseml.pytorch.optim.quantization.quantize_qat_export
remove_node_and_params_from_graph(model, remove_node)
-[docs]def quantize_torch_qat_export(model: ModelProto, inplace: bool = True) -> ModelProto:
+[docs]def quantize_torch_qat_export(
+ model: Union[ModelProto, str],
+ output_file_path: Union[str, None] = None,
+ inplace: bool = True,
+) -> ModelProto:
"""
- :param model: The model to convert
+ :param model: The model to convert, or a file path to it
+ :param output_file_path: File path to save the converted model to
:param inplace: If true, does conversion of model in place. Default is true
:return: Converts a model exported from a torch QAT session from a QAT graph with
fake quantize ops surrounding operations to a quantized graph with quantized
operations. All quantized Convs and FC inputs and outputs be surrounded by
fake quantize ops
"""
+ if isinstance(model, str):
+ model = onnx.load(model)
+
if not inplace:
model = deepcopy(model)
@@ -765,6 +774,9 @@ Source code for sparseml.pytorch.optim.quantization.quantize_qat_export
quantize_resnet_identity_add_inputs(model)
_remove_duplicate_quantize__ops(model)
+ if output_file_path:
+ onnx.save(model, output_file_path)
+
return model
diff --git a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_as.html b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_as.html
index 778115f1370..45032c96317 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_as.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_as.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_lr.html b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_lr.html
index 0fb132f877e..4e8b5220173 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_lr.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_lr.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_pruning.html b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_pruning.html
index d53a2208e3f..ef4da2029e7 100644
--- a/sparseml/_modules/sparseml/pytorch/optim/sensitivity_pruning.html
+++ b/sparseml/_modules/sparseml/pytorch/optim/sensitivity_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/benchmarker.html b/sparseml/_modules/sparseml/pytorch/utils/benchmarker.html
index 1c7497cb0ae..9a13212ce8b 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/benchmarker.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/benchmarker.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/exporter.html b/sparseml/_modules/sparseml/pytorch/utils/exporter.html
index bb757ba9fdc..f5421e57774 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/exporter.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/exporter.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/helpers.html b/sparseml/_modules/sparseml/pytorch/utils/helpers.html
index bb3ea641ba9..576b2686d66 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/helpers.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/logger.html b/sparseml/_modules/sparseml/pytorch/utils/logger.html
index 1f21b04d1d9..6685af7f4c0 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/logger.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/logger.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/loss.html b/sparseml/_modules/sparseml/pytorch/utils/loss.html
index 29388f08303..d9289cdee99 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/loss.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/loss.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/model.html b/sparseml/_modules/sparseml/pytorch/utils/model.html
index 7bddeffd30d..774b0c68747 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/model.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/model.html
@@ -98,16 +98,17 @@
-
+
@@ -200,6 +201,7 @@ Source code for sparseml.pytorch.utils.model
from torch.optim.optimizer import Optimizer
from sparseml.utils.helpers import create_parent_dirs
+from sparsezoo import Zoo
try:
@@ -233,7 +235,10 @@ Source code for sparseml.pytorch.utils.model
"""
Load the state dict into a model from a given file.
- :param path: the path to the pth file to load the state dict from
+ :param path: the path to the pth file to load the state dict from.
+ May also be a SparseZoo stub path preceded by 'zoo:' with the optional
+ `?recipe_type=` argument. If given a recipe type, the base model weights
+ for that recipe will be loaded.
:param model: the model to load the state dict into
:param strict: True to enforce that all tensors match between the model
and the file; False otherwise
@@ -243,6 +248,15 @@ Source code for sparseml.pytorch.utils.model
look like they came from DataParallel type setup (start with module.).
This removes "module." all keys
"""
+ if path.startswith("zoo:"):
+ if "recipe_type=" in path:
+ path = Zoo.download_recipe_base_framework_files(path, extensions=[".pth"])[
+ 0
+ ]
+ else:
+ path = Zoo.load_model_from_stub(path).download_framework_files(
+ extensions=[".pth"]
+ )[0]
model_dict = torch.load(path, map_location="cpu")
current_dict = model.state_dict()
diff --git a/sparseml/_modules/sparseml/pytorch/utils/module.html b/sparseml/_modules/sparseml/pytorch/utils/module.html
index 4504a5a58ae..9bf1884a106 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/module.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/module.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/ssd_helpers.html b/sparseml/_modules/sparseml/pytorch/utils/ssd_helpers.html
index a720efcfbae..fa6ad025e3a 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/ssd_helpers.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/ssd_helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/pytorch/utils/yolo_helpers.html b/sparseml/_modules/sparseml/pytorch/utils/yolo_helpers.html
index 4efa2bc6bd8..3122269f339 100644
--- a/sparseml/_modules/sparseml/pytorch/utils/yolo_helpers.html
+++ b/sparseml/_modules/sparseml/pytorch/utils/yolo_helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/cifar.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/cifar.html
index 6ff051793fd..eae5cab7ab4 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/cifar.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/cifar.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagefolder.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagefolder.html
index 4eca0d4ce0a..0b3d18546cd 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagefolder.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagefolder.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenet.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenet.html
index bd3e41d04d4..8881a275631 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenet.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenette.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenette.html
index 7d60fdbb8bb..2cbb0abcd9f 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenette.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/classification/imagenette.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/dataset.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/dataset.html
index 9fab2875c4c..cf0091312e0 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/dataset.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/dataset.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/helpers.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/helpers.html
index aea83a16b25..469b26e2edf 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/helpers.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/datasets/registry.html b/sparseml/_modules/sparseml/tensorflow_v1/datasets/registry.html
index c4c029845ca..4ff2bfd6723 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/datasets/registry.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/datasets/registry.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mnist.html b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mnist.html
index 7db5b2936c2..abf37148efe 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mnist.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mnist.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet.html b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet.html
index 70965fe44ee..9571d572122 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet_v2.html b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet_v2.html
index 59671abd317..6c992ec77e3 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet_v2.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/mobilenet_v2.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/resnet.html b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/resnet.html
index a701161df7d..56d349af9fa 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/resnet.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/resnet.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/vgg.html b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/vgg.html
index 425034550cb..295513bbece 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/classification/vgg.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/classification/vgg.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/estimator.html b/sparseml/_modules/sparseml/tensorflow_v1/models/estimator.html
index 7305c4554ae..4599ba94416 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/estimator.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/estimator.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/models/registry.html b/sparseml/_modules/sparseml/tensorflow_v1/models/registry.html
index 3e2a27cc877..606d949dd99 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/models/registry.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/models/registry.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/nn/layers.html b/sparseml/_modules/sparseml/tensorflow_v1/nn/layers.html
index 4da12e5c26e..2674cb09900 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/nn/layers.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/nn/layers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/analyzer_module.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/analyzer_module.html
index 31582575f01..b109912a8ce 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/analyzer_module.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/analyzer_module.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/manager.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/manager.html
index b8879c15e1c..f9900f2ab51 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/manager.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/manager.html
@@ -98,16 +98,17 @@
-
+
@@ -201,6 +202,7 @@ Source code for sparseml.tensorflow_v1.optim.manager
from sparseml.tensorflow_v1.optim.modifier import NM_RECAL, Modifier, ScheduledModifier
from sparseml.tensorflow_v1.utils import tf_compat
from sparseml.utils import load_recipe_yaml_str
+from sparsezoo.objects import OptimizationRecipe
__all__ = ["ScheduledModifierManager"]
@@ -250,15 +252,23 @@ Source code for sparseml.tensorflow_v1.optim.manager
"""
[docs] @staticmethod
- def from_yaml(file_path: str, add_modifiers: List[Modifier] = None):
+ def from_yaml(
+ file_path: Union[str, OptimizationRecipe],
+ add_modifiers: List[Modifier] = None,
+ ):
"""
- Convenience function used to create the manager of multiple modifiers
- from a yaml file.
-
- :param file_path: the path to the yaml file to load the modifier from
+ Convenience function used to create the manager of multiple modifiers from a
+ recipe file.
+
+ :param file_path: the path to the recipe file to load the modifier from, or
+ a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+ SparseZoo stubs should be preceded by 'zoo:', and can contain an optional
+ '?recipe_type=<type>' parameter. Can also be a SparseZoo OptimizationRecipe
+ object. i.e. '/path/to/local/recipe.yaml', 'zoo:model/stub/path',
+ 'zoo:model/stub/path?recipe_type=transfer'
:param add_modifiers: additional modifiers that should be added to the
- returned manager alongside the ones loaded from the yaml file
- :return: ScheduledModifierManager() created from the yaml file
+ returned manager alongside the ones loaded from the recipe file
+ :return: ScheduledModifierManager() created from the recipe file
"""
yaml_str = load_recipe_yaml_str(file_path)
modifiers = Modifier.load_list(yaml_str)
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_creator_pruning.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_creator_pruning.html
index e4708162982..824d7753763 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_creator_pruning.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_creator_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_pruning.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_pruning.html
index 68632c9bc54..e03b0a825bc 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_pruning.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/mask_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier.html
index d90f4954e3b..90e71a013c4 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_epoch.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_epoch.html
index 0aa6afc35f1..65434fa1892 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_epoch.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_epoch.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_lr.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_lr.html
index 44ac066483f..b2104396fc6 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_lr.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_lr.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_params.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_params.html
index 951067476d5..3cc3d80ac67 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_params.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_params.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_pruning.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_pruning.html
index 60df3b6f2ba..bed16ce6517 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_pruning.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/modifier_pruning.html
@@ -98,16 +98,17 @@
-
+
@@ -232,7 +233,7 @@ Source code for sparseml.tensorflow_v1.optim.modifier_pruning
Useful for transfer learning use cases.
| Sample yaml:
- | !ConstantKSModifier
+ | !ConstantPruningModifier
| params: __ALL__
| start_epoch: 0.0
| end_epoch: 10.0
@@ -413,7 +414,7 @@ Source code for sparseml.tensorflow_v1.optim.modifier_pruning
Applies based on magnitude pruning without any structure to the pruning.
| Sample yaml:
- | !GradualKSModifier
+ | !GMPruningModifier
| params: __ALL__
| init_sparsity: 0.05
| final_sparsity: 0.8
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/schedule_lr.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/schedule_lr.html
index 8df66f4e752..1c8c3027888 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/schedule_lr.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/schedule_lr.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/optim/sensitivity_pruning.html b/sparseml/_modules/sparseml/tensorflow_v1/optim/sensitivity_pruning.html
index 1fc09fa11cb..c7a37144591 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/optim/sensitivity_pruning.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/optim/sensitivity_pruning.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/utils/exporter.html b/sparseml/_modules/sparseml/tensorflow_v1/utils/exporter.html
index db4230fe0bb..63853b5a570 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/utils/exporter.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/utils/exporter.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/utils/loss.html b/sparseml/_modules/sparseml/tensorflow_v1/utils/loss.html
index 5359c277c2e..1fbab0c9964 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/utils/loss.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/utils/loss.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/utils/nets_utils.html b/sparseml/_modules/sparseml/tensorflow_v1/utils/nets_utils.html
index e21a6b4771b..4ed80c11b7a 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/utils/nets_utils.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/utils/nets_utils.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/utils/summary.html b/sparseml/_modules/sparseml/tensorflow_v1/utils/summary.html
index 980d1e9e5a9..4b5258fb5b5 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/utils/summary.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/utils/summary.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/tensorflow_v1/utils/variable.html b/sparseml/_modules/sparseml/tensorflow_v1/utils/variable.html
index 17431faeb29..690e17a5c07 100644
--- a/sparseml/_modules/sparseml/tensorflow_v1/utils/variable.html
+++ b/sparseml/_modules/sparseml/tensorflow_v1/utils/variable.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/utils/datasets/helpers.html b/sparseml/_modules/sparseml/utils/datasets/helpers.html
index 242d08a19b6..1afa76014e9 100644
--- a/sparseml/_modules/sparseml/utils/datasets/helpers.html
+++ b/sparseml/_modules/sparseml/utils/datasets/helpers.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/utils/datasets/imagenette.html b/sparseml/_modules/sparseml/utils/datasets/imagenette.html
index 9a9b92b8fbc..ac59f0862a5 100644
--- a/sparseml/_modules/sparseml/utils/datasets/imagenette.html
+++ b/sparseml/_modules/sparseml/utils/datasets/imagenette.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/utils/helpers.html b/sparseml/_modules/sparseml/utils/helpers.html
index ea36ce39182..0f533fcd6b6 100644
--- a/sparseml/_modules/sparseml/utils/helpers.html
+++ b/sparseml/_modules/sparseml/utils/helpers.html
@@ -98,16 +98,17 @@
-
+
@@ -205,6 +206,8 @@ Source code for sparseml.utils.helpers
import numpy
+from sparsezoo import Zoo
+from sparsezoo.objects import OptimizationRecipe
from sparsezoo.utils import load_numpy_list
@@ -939,16 +942,30 @@ Source code for sparseml.utils.helpers
)
-[docs]def load_recipe_yaml_str(file_path: str) -> str:
+[docs]def load_recipe_yaml_str(file_path: Union[str, OptimizationRecipe]) -> str:
"""
Loads a YAML recipe file to a string or
extracts recipe from YAML front matter in a sparsezoo markdown recipe card.
+ Recipes can also be provided as SparseZoo model stubs or OptimizationRecipe
+ objects.
YAML front matter: https://jekyllrb.com/docs/front-matter/
- :param file_path: file path to recipe YAML file or markdown recipe card
+ :param file_path: file path to recipe YAML file or markdown recipe card or
+ stub to a SparseZoo model whose recipe will be downloaded and loaded.
+ SparseZoo stubs should be preceded by 'zoo:', and can contain an optional
+ '?recipe_type=<type>' parameter. Can also be a SparseZoo OptimizationRecipe
+ object. i.e. '/path/to/local/recipe.yaml', 'zoo:model/stub/path',
+ 'zoo:model/stub/path?recipe_type=transfer'
:return: the recipe YAML configuration loaded as a string
"""
+ if isinstance(file_path, OptimizationRecipe):
+ # download and unwrap OptimizationRecipe object
+ file_path = file_path.downloaded_path()
+ elif file_path.startswith("zoo:"):
+ # download from zoo stub
+ file_path = Zoo.download_recipe_from_stub(file_path)
+
extension = file_path.lower().split(".")[-1]
if extension not in ["md", "yaml"]:
raise ValueError(
diff --git a/sparseml/_modules/sparseml/utils/singleton.html b/sparseml/_modules/sparseml/utils/singleton.html
index 83658e1bd52..eb6e2837717 100644
--- a/sparseml/_modules/sparseml/utils/singleton.html
+++ b/sparseml/_modules/sparseml/utils/singleton.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/utils/worker.html b/sparseml/_modules/sparseml/utils/worker.html
index ce85cada75c..2ba3a469293 100644
--- a/sparseml/_modules/sparseml/utils/worker.html
+++ b/sparseml/_modules/sparseml/utils/worker.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/sparseml/utils/wrapper.html b/sparseml/_modules/sparseml/utils/wrapper.html
index da529aadd11..6c623873a0e 100644
--- a/sparseml/_modules/sparseml/utils/wrapper.html
+++ b/sparseml/_modules/sparseml/utils/wrapper.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/_modules/tensorflow/python/ops/math_ops.html b/sparseml/_modules/tensorflow/python/ops/math_ops.html
index 2574647da6e..119591176b7 100644
--- a/sparseml/_modules/tensorflow/python/ops/math_ops.html
+++ b/sparseml/_modules/tensorflow/python/ops/math_ops.html
@@ -98,16 +98,17 @@
-
+
@@ -258,10 +259,8 @@ Source code for tensorflow.python.ops.math_ops
from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import tensor_shape
-from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
-from tensorflow.python.ops import gen_bitwise_ops
from tensorflow.python.ops import gen_data_flow_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import gen_nn_ops
@@ -275,131 +274,16 @@
Source code for tensorflow.python.ops.math_ops
from tensorflow.python.util import deprecation
from tensorflow.python.util import dispatch
from tensorflow.python.util import nest
-from tensorflow.python.util.compat import collections_abc
from tensorflow.python.util.tf_export import tf_export
# Aliases for some automatically-generated names.
+linspace = gen_math_ops.lin_space
nextafter = gen_math_ops.next_after
-
-@tf_export("linspace", v1=["lin_space", "linspace"])
-@dispatch.add_dispatch_support
-@deprecation.deprecated_endpoints("lin_space")
-def linspace_nd(start, stop, num, name=None, axis=0):
- r"""Generates evenly-spaced values in an interval along a given axis.
-
- A sequence of `num` evenly-spaced values are generated beginning at `start`
- along a given `axis`.
- If `num > 1`, the values in the sequence increase by `stop - start / num - 1`,
- so that the last one is exactly `stop`. If `num <= 0`, `ValueError` is raised.
-
- Matches
- [np.linspace](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html)'s
- behaviour
- except when `num == 0`.
-
- For example:
-
- ```
- tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0 11.0 12.0]
- ```
-
- `Start` and `stop` can be tensors of arbitrary size:
-
- >>> tf.linspace([0., 5.], [10., 40.], 5, axis=0)
- <tf.Tensor: shape=(5, 2), dtype=float32, numpy=
- array([[ 0. , 5. ],
- [ 2.5 , 13.75],
- [ 5. , 22.5 ],
- [ 7.5 , 31.25],
- [10. , 40. ]], dtype=float32)>
-
- `Axis` is where the values will be generated (the dimension in the
- returned tensor which corresponds to the axis will be equal to `num`)
-
- >>> tf.linspace([0., 5.], [10., 40.], 5, axis=-1)
- <tf.Tensor: shape=(2, 5), dtype=float32, numpy=
- array([[ 0. , 2.5 , 5. , 7.5 , 10. ],
- [ 5. , 13.75, 22.5 , 31.25, 40. ]], dtype=float32)>
-
-
-
- Args:
- start: A `Tensor`. Must be one of the following types: `bfloat16`,
- `float32`, `float64`. N-D tensor. First entry in the range.
- stop: A `Tensor`. Must have the same type and shape as `start`. N-D tensor.
- Last entry in the range.
- num: A `Tensor`. Must be one of the following types: `int32`, `int64`. 0-D
- tensor. Number of values to generate.
- name: A name for the operation (optional).
- axis: Axis along which the operation is performed (used only when N-D
- tensors are provided).
-
- Returns:
- A `Tensor`. Has the same type as `start`.
- """
-
- with ops.name_scope(name, "linspace", [start, stop]):
- start = ops.convert_to_tensor(start, name="start")
- # stop must be convertible to the same dtype as start
- stop = ops.convert_to_tensor(stop, name="stop", dtype=start.dtype)
- num_int = array_ops.convert_to_int_tensor(num, name="num")
- num = cast(num_int, dtype=start.dtype)
-
- broadcast_shape = array_ops.broadcast_dynamic_shape(
- array_ops.shape(start), array_ops.shape(stop))
- start = array_ops.broadcast_to(start, broadcast_shape)
- stop = array_ops.broadcast_to(stop, broadcast_shape)
-
- expanded_start = array_ops.expand_dims(start, axis=axis)
- expanded_stop = array_ops.expand_dims(stop, axis=axis)
-
- shape = array_ops.shape(expanded_start)
- ndims = array_ops.shape(shape)[0]
-
- axis = array_ops.where_v2(axis >= 0, axis, ndims + axis)
-
- # The purpose is to avoid having negative values when repeating.
- num_fill = gen_math_ops.maximum(num_int - 2, 0)
- # To avoid having negative values in the range or zero division
- # the result is sliced in the end so a correct result is returned for
- # num == 1, and num == 0.
- n_steps = gen_math_ops.maximum(num_int - 1, 1)
- delta = (expanded_stop - expanded_start) / cast(n_steps,
- expanded_stop.dtype)
- # Re-cast tensors as delta.
- expanded_start = cast(expanded_start, delta.dtype)
- expanded_stop = cast(expanded_stop, delta.dtype)
- # If num < 0, we will throw exception in the range
- # otherwise use the same div for delta
- range_end = array_ops.where_v2(num_int >= 0, n_steps, -1)
- # Even though range supports an output dtype, its limited
- # (e.g. doesn't support half at the moment).
- desired_range = cast(range(1, range_end, dtype=dtypes.int64), delta.dtype)
- mask = gen_math_ops.equal(axis, range(ndims))
- # desired_range_shape is [1. 1. 1. ... 1. num_fill 1. 1. ... 1.], where the
- # index of num_fill is equal to axis.
- desired_range_shape = array_ops.where_v2(mask, num_fill, 1)
- desired_range = array_ops.reshape(desired_range, desired_range_shape)
-
- res = expanded_start + delta * desired_range
-
- # Add the start and endpoints to the result, and slice out the desired
- # portion.
- all_tensors = (expanded_start, res, expanded_stop)
- concatenated = array_ops.concat(all_tensors, axis=axis)
- begin = array_ops.zeros_like(shape)
- size = array_ops.where_v2(mask, num_int, shape)
-
- return array_ops.slice(concatenated, begin, size)
-
-
-linspace = linspace_nd
-
arg_max = deprecation.deprecated(None, "Use `tf.math.argmax` instead")(arg_max) # pylint: disable=used-before-assignment
arg_min = deprecation.deprecated(None, "Use `tf.math.argmin` instead")(arg_min) # pylint: disable=used-before-assignment
-tf_export(v1=["arg_max"])(dispatch.add_dispatch_support(arg_max))
-tf_export(v1=["arg_min"])(dispatch.add_dispatch_support(arg_min))
+tf_export(v1=["arg_max"])(arg_max)
+tf_export(v1=["arg_min"])(arg_min)
# This is set by resource_variable_ops.py. It is included in this way since
@@ -418,7 +302,6 @@
Source code for tensorflow.python.ops.math_ops
# pylint: disable=redefined-builtin
@tf_export(v1=["math.argmax", "argmax"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None, "Use the `axis` argument instead",
"dimension")
@_set_doc(
@@ -435,11 +318,10 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.argmax", "argmax", v1=[])
-@dispatch.add_dispatch_support
def argmax_v2(input, axis=None, output_type=dtypes.int64, name=None):
"""Returns the index with the largest value across axes of a tensor.
- In case of identity returns the smallest index.
+ Note that in case of ties the identity of the return value is not guaranteed.
For example:
@@ -452,9 +334,6 @@
Source code for tensorflow.python.ops.math_ops
<tf.Tensor: shape=(5,), dtype=int64, numpy=array([2, 2, 0, 2, 2])>
>>> tf.math.argmax(B, 1)
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([2, 2, 1])>
- >>> C = tf.constant([0, 0, 0, 0])
- >>> tf.math.argmax(C) # Returns smallest index in case of ties
- <tf.Tensor: shape=(), dtype=int64, numpy=0>
Args:
input: A `Tensor`.
@@ -472,7 +351,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.argmin", "argmin"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None, "Use the `axis` argument instead",
"dimension")
@_set_doc(
@@ -489,11 +367,10 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.argmin", "argmin", v1=[])
-@dispatch.add_dispatch_support
def argmin_v2(input, axis=None, output_type=dtypes.int64, name=None):
"""Returns the index with the smallest value across axes of a tensor.
- Returns the smallest index in case of ties.
+ Note that in case of ties the identity of the return value is not guaranteed.
Args:
input: A `Tensor`. Must be one of the following types: `float32`, `float64`,
@@ -542,18 +419,9 @@
Source code for tensorflow.python.ops.math_ops
Given a tensor `x` of complex numbers, this operation returns a tensor of type
`float32` or `float64` that is the absolute value of each element in `x`. For
- a complex number \\(a + bj\\), its absolute value is computed as
- \\(\sqrt{a^2 + b^2}\\).
+ a complex number \\(a + bj\\), its absolute value is computed as \\(\sqrt{a^2
+ + b^2}\\). For example:
- For example:
-
- >>> # real number
- >>> x = tf.constant([-2.25, 3.25])
- >>> tf.abs(x)
- <tf.Tensor: shape=(2,), dtype=float32,
- numpy=array([2.25, 3.25], dtype=float32)>
-
- >>> # complex number
>>> x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]])
>>> tf.abs(x)
<tf.Tensor: shape=(2, 1), dtype=float64, numpy=
@@ -638,10 +506,6 @@
Source code for tensorflow.python.ops.math_ops
# override names. Use a dummy class to track the runtime division behavior
return DivideDelegateWithName(x, name) / y
else:
- # We do conversion here to make sure at least x is a tensor.
- if not tensor_util.is_tensor(x):
- dtype = y.dtype.base_dtype if tensor_util.is_tensor(y) else None
- x = ops.convert_to_tensor(x, dtype=dtype)
return x / y
@@ -709,31 +573,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.subtract", "subtract")
@dispatch.add_dispatch_support
def subtract(x, y, name=None):
- """Returns x - y element-wise.
-
- *Note*: Subtract supports broadcasting. More about broadcasting
- [here](https://numpy.org/doc/stable/user/basics.broadcasting.html)
-
- Both input and output have a range `(-inf, inf)`.
-
- For example:
-
- >>> x = tf.constant([1.0, -1.0, 5.0, -2.0, 0.0])
- >>> y = tf.constant([5.0, 1.0, 3.7, -19.9, float("inf")])
- >>> tf.subtract(x,y)
- <tf.Tensor: shape=(5,), dtype=float32,
- numpy= array([-4. , -2. , 1.3, 17.9, -inf], dtype=float32)>
-
- Args:
- x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`,
- `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`,
- `complex64`, `complex128`, `string`.
- y: A `Tensor`. Must have the same type as x.
- name: A name for the operation (optional).
-
- Returns:
- A `Tensor`. Has the same type as x.
- """
return gen_math_ops.sub(x, y, name)
@@ -778,7 +617,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.scalar_mul", "scalar_mul"])
-@dispatch.add_dispatch_support
def scalar_mul(scalar, x, name=None):
"""Multiplies a scalar times a `Tensor` or `IndexedSlices` object.
@@ -811,7 +649,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.scalar_mul", "scalar_mul", v1=[])
-@dispatch.add_dispatch_support
@_set_doc(scalar_mul.__doc__)
def scalar_mul_v2(scalar, x, name=None):
with ops.name_scope(name, "scalar_mul", [x]) as name:
@@ -895,27 +732,20 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.sign", "sign")
@dispatch.add_dispatch_support
def sign(x, name=None):
- r"""Returns an element-wise indication of the sign of a number.
+ """Returns an element-wise indication of the sign of a number.
- `y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0`.
+ y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
- For complex numbers, `y = sign(x) = x / |x| if x != 0, otherwise y = 0`.
+ For complex numbers, y = sign(x) = x / |x| if x != 0, otherwise y = 0.
Example usage:
- >>> # real number
>>> tf.math.sign([0., 2., -3.])
- <tf.Tensor: shape=(3,), dtype=float32,
- numpy=array([ 0., 1., -1.], dtype=float32)>
-
- >>> # complex number
- >>> tf.math.sign([1 + 1j, 0 + 0j])
- <tf.Tensor: shape=(2,), dtype=complex128,
- numpy=array([0.70710678+0.70710678j, 0. +0.j ])>
+ <tf.Tensor: ... numpy=array([ 0., 1., -1.], dtype=float32)>
Args:
x: A Tensor. Must be one of the following types: bfloat16, half, float32,
- float64, int32, int64, complex64, complex128.
+ float64, int32, int64, complex64, complex128.
name: A name for the operation (optional).
Returns:
@@ -925,7 +755,7 @@
Source code for tensorflow.python.ops.math_ops
tf.math.sign(x.values, ...), x.dense_shape).
"""
x = ops.convert_to_tensor(x)
- if x.dtype.is_complex:
+ if x.dtype in (dtypes.complex64, dtypes.complex128):
return gen_math_ops.div_no_nan(
x,
cast(
@@ -939,7 +769,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.real", v1=["math.real", "real"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("real")
@dispatch.add_dispatch_support
def real(input, name=None):
@@ -974,7 +803,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.imag", v1=["math.imag", "imag"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("imag")
@dispatch.add_dispatch_support
def imag(input, name=None):
@@ -1008,7 +836,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.angle", v1=["math.angle", "angle"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("angle")
@dispatch.add_dispatch_support
def angle(input, name=None):
@@ -1045,8 +872,7 @@
Source code for tensorflow.python.ops.math_ops
if input.dtype.is_complex:
return gen_math_ops.angle(input, Tout=input.dtype.real_dtype, name=name)
else:
- return array_ops.where(input < 0, np.pi * array_ops.ones_like(input),
- array_ops.zeros_like(input))
+ return array_ops.zeros_like(input)
# pylint: enable=redefined-outer-name,redefined-builtin
@@ -1103,8 +929,6 @@
Source code for tensorflow.python.ops.math_ops
returned value is set to `0`. The handling of complex types here matches the
behavior of numpy.
- Note casting nan and inf values to integral types has undefined behavior.
-
Args:
x: A `Tensor` or `SparseTensor` or `IndexedSlices` of numeric type. It could
be `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`,
@@ -1180,7 +1004,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_float"])
-@dispatch.add_dispatch_support
def to_float(x, name="ToFloat"):
"""Casts a tensor to type `float32`.
@@ -1200,7 +1023,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_double"])
-@dispatch.add_dispatch_support
def to_double(x, name="ToDouble"):
"""Casts a tensor to type `float64`.
@@ -1220,7 +1042,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_int32"])
-@dispatch.add_dispatch_support
def to_int32(x, name="ToInt32"):
"""Casts a tensor to type `int32`.
@@ -1240,7 +1061,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_int64"])
-@dispatch.add_dispatch_support
def to_int64(x, name="ToInt64"):
"""Casts a tensor to type `int64`.
@@ -1260,7 +1080,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_bfloat16"])
-@dispatch.add_dispatch_support
def to_bfloat16(x, name="ToBFloat16"):
"""Casts a tensor to type `bfloat16`.
@@ -1280,7 +1099,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_complex64"])
-@dispatch.add_dispatch_support
def to_complex64(x, name="ToComplex64"):
"""Casts a tensor to type `complex64`.
@@ -1300,7 +1118,6 @@
Source code for tensorflow.python.ops.math_ops
@deprecation.deprecated(date=None, instructions="Use `tf.cast` instead.")
@tf_export(v1=["to_complex128"])
-@dispatch.add_dispatch_support
def to_complex128(x, name="ToComplex128"):
"""Casts a tensor to type `complex128`.
@@ -1320,6 +1137,10 @@
Source code for tensorflow.python.ops.math_ops
ops.Tensor._override_operator("__neg__", gen_math_ops.neg)
ops.Tensor._override_operator("__abs__", abs)
+# __invert__ corresponds to the ~ operator. Here we follow the numpy convention
+# ~ marks an elementwise bit-wise inverse. This is only implemented for boolean
+# tensors and will throw a TypeError if used on nonboolean arrays
+ops.Tensor._override_operator("__invert__", gen_math_ops.logical_not)
def _OverrideBinaryOperatorHelper(func, op_name, clazz_object=ops.Tensor):
@@ -1336,26 +1157,21 @@
Source code for tensorflow.python.ops.math_ops
def binary_op_wrapper(x, y):
with ops.name_scope(None, op_name, [x, y]) as name:
- try:
+ if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
return func(x, y, name=name)
- except (TypeError, ValueError) as e:
- # Even if dispatching the op failed, the RHS may be a tensor aware
- # object that can implement the operator with knowledge of itself
- # and the tensor.
- # If the RHS is not tensor aware we still want to raise the
- # original error from the LHS, because it may be more
- # informative.
- if hasattr(type(y), "__r%s__" % op_name):
- try:
- r_op = getattr(y, "__r%s__" % op_name)
- out = r_op(x)
- if out is NotImplemented:
- raise
- return out
- except (TypeError, ValueError):
- raise e
- else:
- raise
+ elif not isinstance(y, sparse_tensor.SparseTensor):
+ try:
+ y = ops.convert_to_tensor_v2(
+ y, dtype_hint=x.dtype.base_dtype, name="y")
+ except TypeError:
+ # If the RHS is not a tensor, it might be a tensor aware object
+ # that can implement the operator with knowledge of itself
+ # and the tensor.
+ if hasattr(type(y), "__r%s__" % op_name):
+ return NotImplemented
+ else:
+ raise
+ return func(x, y, name=name)
def binary_op_wrapper_sparse(sp_x, y):
with ops.name_scope(None, op_name, [sp_x, y]) as name:
@@ -1435,7 +1251,7 @@
Source code for tensorflow.python.ops.math_ops
def _truediv_python3(x, y, name=None):
with ops.name_scope(name, "truediv", [x, y]) as name:
x = ops.convert_to_tensor(x, name="x")
- y = ops.convert_to_tensor(y, dtype_hint=x.dtype.base_dtype, name="y")
+ y = ops.convert_to_tensor(y, name="y")
x_dtype = x.dtype.base_dtype
y_dtype = y.dtype.base_dtype
if x_dtype != y_dtype:
@@ -1516,7 +1332,6 @@
Source code for tensorflow.python.ops.math_ops
date=None,
instructions="Deprecated in favor of operator or tf.math.divide.")
@tf_export(v1=["div"])
-@dispatch.add_dispatch_support
def div(x, y, name=None):
"""Divides x / y elementwise (using Python 2 division operator semantics).
@@ -1540,7 +1355,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.divide_no_nan", v1=["math.divide_no_nan", "div_no_nan"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("div_no_nan")
@dispatch.add_dispatch_support
def div_no_nan(x, y, name=None):
@@ -1631,31 +1445,8 @@
Source code for tensorflow.python.ops.math_ops
floormod = gen_math_ops.floor_mod
-@tf_export("__operators__.add", v1=[])
-@dispatch.add_dispatch_support
def _add_dispatch(x, y, name=None):
- """The operation invoked by the `Tensor.__add__` operator.
-
- Purpose in the API:
-
- This method is exposed in TensorFlow's API so that library developers
- can register dispatching for `Tensor.__add__` to allow it to handle
- custom composite tensors & other custom objects.
-
- The API symbol is not intended to be called by users directly and does
- appear in TensorFlow's generated documentation.
-
- Args:
- x: The left-hand side of the `+` operator.
- y: The right-hand side of the `+` operator.
- name: an optional name for the operation.
-
- Returns:
- The result of the elementwise `+` operation.
- """
- if not isinstance(y, ops.Tensor) and not isinstance(
- y, sparse_tensor.SparseTensor):
- y = ops.convert_to_tensor(y, dtype_hint=x.dtype.base_dtype, name="y")
+ """Dispatches to add for strings and add_v2 for all other types."""
if x.dtype == dtypes.string:
return gen_math_ops.add(x, y, name=name)
else:
@@ -1664,12 +1455,14 @@
Source code for tensorflow.python.ops.math_ops
def _mul_dispatch(x, y, name=None):
"""Dispatches cwise mul for "Dense*Dense" and "Dense*Sparse"."""
- if isinstance(y, sparse_tensor.SparseTensor): # Case: Dense * Sparse.
+ is_tensor_y = isinstance(y, ops.Tensor)
+ if is_tensor_y:
+ return gen_math_ops.mul(x, y, name=name)
+ else:
+ assert isinstance(y, sparse_tensor.SparseTensor) # Case: Dense * Sparse.
new_vals = gen_sparse_ops.sparse_dense_cwise_mul(y.indices, y.values,
y.dense_shape, x, name)
return sparse_tensor.SparseTensor(y.indices, new_vals, y.dense_shape)
- else:
- return multiply(x, y, name=name)
# NOTE(aselle): When integer division is added for sparse_dense_cwise,
@@ -1683,10 +1476,10 @@
Source code for tensorflow.python.ops.math_ops
sparse_tensor.SparseTensor)
_OverrideBinaryOperatorHelper(_add_dispatch, "add")
-_OverrideBinaryOperatorHelper(subtract, "sub")
+_OverrideBinaryOperatorHelper(gen_math_ops.sub, "sub")
_OverrideBinaryOperatorHelper(_mul_dispatch, "mul")
-_OverrideBinaryOperatorHelper(div, "div")
-_OverrideBinaryOperatorHelper(truediv, "truediv")
+_OverrideBinaryOperatorHelper(_div_python2, "div")
+_OverrideBinaryOperatorHelper(_truediv_python3, "truediv")
_OverrideBinaryOperatorHelper(floordiv, "floordiv")
_OverrideBinaryOperatorHelper(gen_math_ops.floor_mod, "mod")
_OverrideBinaryOperatorHelper(pow, "pow")
@@ -1783,35 +1576,9 @@
Source code for tensorflow.python.ops.math_ops
return gen_math_ops.logical_and(x, y, name)
-def and_(x, y, name=None):
- if x.dtype == dtypes.bool:
- return gen_math_ops.logical_and(x, y, name)
- return gen_bitwise_ops.bitwise_and(x, y)
-
-
-def or_(x, y, name=None):
- if x.dtype == dtypes.bool:
- return gen_math_ops.logical_or(x, y, name)
- return gen_bitwise_ops.bitwise_or(x, y)
-
-
-def xor_(x, y, name=None):
- if x.dtype == dtypes.bool:
- return logical_xor(x, y, name)
- return gen_bitwise_ops.bitwise_xor(x, y)
-
-
-def invert_(x, name=None):
- if x.dtype == dtypes.bool:
- return gen_math_ops.logical_not(x, name=name)
- return gen_bitwise_ops.invert(x, name=name)
-
-
-_OverrideBinaryOperatorHelper(and_, "and")
-_OverrideBinaryOperatorHelper(or_, "or")
-_OverrideBinaryOperatorHelper(xor_, "xor")
-ops.Tensor._override_operator("__invert__", invert_)
-
+_OverrideBinaryOperatorHelper(gen_math_ops.logical_and, "and")
+_OverrideBinaryOperatorHelper(gen_math_ops.logical_or, "or")
+_OverrideBinaryOperatorHelper(logical_xor, "xor")
ops.Tensor._override_operator("__lt__", gen_math_ops.less)
ops.Tensor._override_operator("__le__", gen_math_ops.less_equal)
@@ -1842,8 +1609,8 @@
Source code for tensorflow.python.ops.math_ops
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, True])>
Args:
- x: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
- y: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
+ x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
+ y: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional).
Returns:
@@ -1878,8 +1645,8 @@
Source code for tensorflow.python.ops.math_ops
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, False])>
Args:
- x: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
- y: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
+ x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
+ y: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional).
Returns:
@@ -1891,33 +1658,8 @@
Source code for tensorflow.python.ops.math_ops
return gen_math_ops.not_equal(x, y, name=name)
-@tf_export("__operators__.eq", v1=[])
-@dispatch.add_dispatch_support
def tensor_equals(self, other):
- """The operation invoked by the `Tensor.__eq__` operator.
-
- Compares two tensors element-wise for equality if they are
- broadcast-compatible; or returns False if they are not broadcast-compatible.
- (Note that this behavior differs from `tf.math.equal`, which raises an
- exception if the two tensors are not broadcast-compatible.)
-
- Purpose in the API:
-
- This method is exposed in TensorFlow's API so that library developers
- can register dispatching for `Tensor.__eq__` to allow it to handle
- custom composite tensors & other custom objects.
-
- The API symbol is not intended to be called by users directly and does
- appear in TensorFlow's generated documentation.
-
- Args:
- self: The left-hand side of the `==` operator.
- other: The right-hand side of the `==` operator.
-
- Returns:
- The result of the elementwise `==` operation, or `False` if the arguments
- are not broadcast-compatible.
- """
+ """Compares two tensors element-wise for equality."""
if other is None:
return False
g = getattr(self, "graph", None)
@@ -1929,33 +1671,8 @@
Source code for tensorflow.python.ops.math_ops
return self is other
-@tf_export("__operators__.ne", v1=[])
-@dispatch.add_dispatch_support
def tensor_not_equals(self, other):
- """The operation invoked by the `Tensor.__ne__` operator.
-
- Compares two tensors element-wise for inequality if they are
- broadcast-compatible; or returns True if they are not broadcast-compatible.
- (Note that this behavior differs from `tf.math.not_equal`, which raises an
- exception if the two tensors are not broadcast-compatible.)
-
- Purpose in the API:
-
- This method is exposed in TensorFlow's API so that library developers
- can register dispatching for `Tensor.__ne__` to allow it to handle
- custom composite tensors & other custom objects.
-
- The API symbol is not intended to be called by users directly and does
- appear in TensorFlow's generated documentation.
-
- Args:
- self: The left-hand side of the `!=` operator.
- other: The right-hand side of the `!=` operator.
-
- Returns:
- The result of the elementwise `!=` operation, or `True` if the arguments
- are not broadcast-compatible.
- """
+ """Compares two tensors element-wise for equality."""
if other is None:
return True
if ops.Tensor._USE_EQUALITY and ops.executing_eagerly_outside_functions():
@@ -1970,7 +1687,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("range")
-@dispatch.add_dispatch_support
def range(start, limit=None, delta=1, dtype=None, name="range"): # pylint: disable=redefined-builtin
"""Creates a sequence of numbers.
@@ -2064,23 +1780,28 @@
Source code for tensorflow.python.ops.math_ops
_range_tensor_conversion_function)
# Reduction operations
-def _ReductionDims(x, axis): # pylint: disable=invalid-name
- """Returns range(0, rank(x)) if axis is None."""
+def _ReductionDims(x, axis, reduction_indices=None): # pylint: disable=invalid-name
+ """Returns range(0, rank(x)) if reduction_indices is None."""
+ # TODO(aselle): Remove this after deprecation
+ if reduction_indices is not None:
+ if axis is not None:
+ raise ValueError("Can't specify both axis' and 'reduction_indices'.")
+ axis = reduction_indices
if axis is not None:
return axis
else:
- x_rank = None
+ # Fast path: avoid creating Rank and Range ops if ndims is known.
if isinstance(x, ops.Tensor):
- x_rank = x.shape.rank
+ rank = x.shape.rank
+ if rank is not None:
+ return constant_op.constant(np.arange(rank, dtype=np.int32))
elif (isinstance(x, sparse_tensor.SparseTensor) and
x.dense_shape.shape.is_fully_defined()):
- x_rank = x.dense_shape.shape.dims[0].value # sparse.dense_shape is 1-D.
- # Fast path: avoid creating Rank and Range ops if ndims is known.
- if x_rank:
- return constant_op.constant(np.arange(x_rank, dtype=np.int32))
- else:
- # Otherwise, we rely on Range and Rank to do the right thing at run-time.
- return range(0, array_ops.rank(x))
+ rank = x.dense_shape.shape.dims[0].value # sparse.dense_shape is 1-D.
+ return constant_op.constant(np.arange(rank, dtype=np.int32))
+
+ # Otherwise, we rely on Range and Rank to do the right thing at run-time.
+ return range(0, array_ops.rank(x))
def _has_fully_defined_shape(tensor):
@@ -2097,7 +1818,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_sum", "reduce_sum"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2111,8 +1831,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2161,49 +1881,28 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
For example:
- >>> # x has a shape of (2, 3) (two rows and three columns):
- >>> x = tf.constant([[1, 1, 1], [1, 1, 1]])
- >>> x.numpy()
- array([[1, 1, 1],
- [1, 1, 1]], dtype=int32)
- >>> # sum all the elements
- >>> # 1 + 1 + 1 + 1 + 1+ 1 = 6
- >>> tf.reduce_sum(x).numpy()
- 6
- >>> # reduce along the first dimension
- >>> # the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
- >>> tf.reduce_sum(x, 0).numpy()
- array([2, 2, 2], dtype=int32)
- >>> # reduce along the second dimension
- >>> # the result is [1, 1] + [1, 1] + [1, 1] = [3, 3]
- >>> tf.reduce_sum(x, 1).numpy()
- array([3, 3], dtype=int32)
- >>> # keep the original dimensions
- >>> tf.reduce_sum(x, 1, keepdims=True).numpy()
- array([[3],
- [3]], dtype=int32)
- >>> # reduce along both dimensions
- >>> # the result is 1 + 1 + 1 + 1 + 1 + 1 = 6
- >>> # or, equivalently, reduce along rows, then reduce the resultant array
- >>> # [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
- >>> # 2 + 2 + 2 = 6
- >>> tf.reduce_sum(x, [0, 1]).numpy()
- 6
-
+ ```python
+ x = tf.constant([[1, 1, 1], [1, 1, 1]])
+ tf.reduce_sum(x) # 6
+ tf.reduce_sum(x, 0) # [2, 2, 2]
+ tf.reduce_sum(x, 1) # [3, 3]
+ tf.reduce_sum(x, 1, keepdims=True) # [[3], [3]]
+ tf.reduce_sum(x, [0, 1]) # 6
+ ```
Args:
input_tensor: The tensor to reduce. Should have numeric type.
axis: The dimensions to reduce. If `None` (the default), reduces all
dimensions. Must be in the range `[-rank(input_tensor),
- rank(input_tensor)]`.
+ rank(input_tensor))`.
keepdims: If true, retains reduced dimensions with length 1.
name: A name for the operation (optional).
@@ -2232,14 +1931,13 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.reduce_euclidean_norm")
-@dispatch.add_dispatch_support
def reduce_euclidean_norm(input_tensor, axis=None, keepdims=False, name=None):
"""Computes the Euclidean norm of elements across dimensions of a tensor.
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2276,7 +1974,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.count_nonzero", "count_nonzero"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2354,7 +2051,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.count_nonzero", v1=[])
-@dispatch.add_dispatch_support
def count_nonzero_v2(
input, # pylint: disable=redefined-builtin
axis=None,
@@ -2422,7 +2118,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_mean", "reduce_mean"])
-@dispatch.add_dispatch_support
def reduce_mean_v1(input_tensor,
axis=None,
keepdims=None,
@@ -2434,8 +2129,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis` by computing the
mean of elements across the dimensions in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a tensor with a single
element is returned.
@@ -2496,8 +2191,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis` by computing the
mean of elements across the dimensions in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions are retained
+ with length 1.
If `axis` is None, all dimensions are reduced, and a tensor with a single
element is returned.
@@ -2549,30 +2244,28 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.reduce_variance")
-@dispatch.add_dispatch_support
def reduce_variance(input_tensor, axis=None, keepdims=False, name=None):
"""Computes the variance of elements across dimensions of a tensor.
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
For example:
- >>> x = tf.constant([[1., 2.], [3., 4.]])
- >>> tf.math.reduce_variance(x)
- <tf.Tensor: shape=(), dtype=float32, numpy=1.25>
- >>> tf.math.reduce_variance(x, 0)
- <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], ...)>
- >>> tf.math.reduce_variance(x, 1)
- <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.25, 0.25], ...)>
+ ```python
+ x = tf.constant([[1., 2.], [3., 4.]])
+ tf.reduce_variance(x) # 1.25
+ tf.reduce_variance(x, 0) # [1., 1.]
+ tf.reduce_variance(x, 1) # [0.25, 0.25]
+ ```
Args:
- input_tensor: The tensor to reduce. Should have real or complex type.
+ input_tensor: The tensor to reduce. Should have numeric type.
axis: The dimensions to reduce. If `None` (the default), reduces all
dimensions. Must be in the range `[-rank(input_tensor),
rank(input_tensor))`.
@@ -2580,60 +2273,47 @@
Source code for tensorflow.python.ops.math_ops
name: A name scope for the associated operations (optional).
Returns:
- The reduced tensor, of the same dtype as the input_tensor. Note, for
- `complex64` or `complex128` input, the returned `Tensor` will be of type
- `float32` or `float64`, respectively.
+ The reduced tensor, of the same dtype as the input_tensor.
@compatibility(numpy)
Equivalent to np.var
- Please note `np.var` has a `dtype` parameter that could be used to specify the
- output type. By default this is `dtype=float64`. On the other hand,
- `tf.math.reduce_variance` has aggressive type inference from `input_tensor`.
+ Please note that `np.var` has a `dtype` parameter that could be used to
+ specify the output type. By default this is `dtype=float64`. On the other
+ hand, `tf.reduce_variance` has an aggressive type inference from
+ `input_tensor`,
@end_compatibility
"""
name = name if name else "reduce_variance"
with ops.name_scope(name):
means = reduce_mean(input_tensor, axis=axis, keepdims=True)
- if means.dtype.is_integer:
- raise TypeError("Input must be either real or complex")
- diff = input_tensor - means
- if diff.dtype.is_complex:
- # For complex values we need to take the absolute value before squaring.
- # This is achieved by multiplying with the conjugate.
- real_dtype = diff.dtype.real_dtype
- squared_deviations = gen_math_ops.real(
- gen_math_ops.mul(gen_math_ops.conj(diff), diff), Tout=real_dtype)
- else:
- squared_deviations = gen_math_ops.square(diff)
+ squared_deviations = gen_math_ops.square(input_tensor - means)
return reduce_mean(squared_deviations, axis=axis, keepdims=keepdims)
@tf_export("math.reduce_std")
-@dispatch.add_dispatch_support
def reduce_std(input_tensor, axis=None, keepdims=False, name=None):
"""Computes the standard deviation of elements across dimensions of a tensor.
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
For example:
- >>> x = tf.constant([[1., 2.], [3., 4.]])
- >>> tf.math.reduce_std(x)
- <tf.Tensor: shape=(), dtype=float32, numpy=1.118034>
- >>> tf.math.reduce_std(x, 0)
- <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
- >>> tf.math.reduce_std(x, 1)
- <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.5, 0.5], dtype=float32)>
+ ```python
+ x = tf.constant([[1., 2.], [3., 4.]])
+ tf.reduce_std(x) # 1.1180339887498949
+ tf.reduce_std(x, 0) # [1., 1.]
+ tf.reduce_std(x, 1) # [0.5, 0.5]
+ ```
Args:
- input_tensor: The tensor to reduce. Should have real or complex type.
+ input_tensor: The tensor to reduce. Should have numeric type.
axis: The dimensions to reduce. If `None` (the default), reduces all
dimensions. Must be in the range `[-rank(input_tensor),
rank(input_tensor))`.
@@ -2641,16 +2321,14 @@
Source code for tensorflow.python.ops.math_ops
name: A name scope for the associated operations (optional).
Returns:
- The reduced tensor, of the same dtype as the input_tensor. Note, for
- `complex64` or `complex128` input, the returned `Tensor` will be of type
- `float32` or `float64`, respectively.
+ The reduced tensor, of the same dtype as the input_tensor.
@compatibility(numpy)
Equivalent to np.std
- Please note `np.std` has a `dtype` parameter that could be used to specify the
- output type. By default this is `dtype=float64`. On the other hand,
- `tf.math.reduce_std` has aggressive type inference from `input_tensor`.
+ Please note that `np.std` has a `dtype` parameter that could be used to
+ specify the output type. By default this is `dtype=float64`. On the other
+ hand, `tf.reduce_std` has an aggressive type inference from `input_tensor`,
@end_compatibility
"""
name = name if name else "reduce_std"
@@ -2696,7 +2374,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_prod", "reduce_prod"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2710,8 +2387,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2742,7 +2419,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_min", "reduce_min"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2756,8 +2432,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2794,8 +2470,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2829,7 +2505,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_max", "reduce_max"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2843,8 +2518,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2881,8 +2556,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2897,10 +2572,10 @@
Source code for tensorflow.python.ops.math_ops
tf.Tensor(-1, shape=(), dtype=int32)
>>> x = tf.constant([4, float('nan')])
>>> print(tf.reduce_max(x))
- tf.Tensor(nan, shape=(), dtype=float32)
+ tf.Tensor(4.0, shape=(), dtype=float32)
>>> x = tf.constant([float('nan'), float('nan')])
>>> print(tf.reduce_max(x))
- tf.Tensor(nan, shape=(), dtype=float32)
+ tf.Tensor(-inf, shape=(), dtype=float32)
>>> x = tf.constant([float('-inf'), float('inf')])
>>> print(tf.reduce_max(x))
tf.Tensor(inf, shape=(), dtype=float32)
@@ -2934,7 +2609,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_all", "reduce_all"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -2948,8 +2622,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -2988,15 +2662,15 @@
Source code for tensorflow.python.ops.math_ops
return reduce_all(input_tensor, axis, keepdims, name)
-@tf_export("math.reduce_all", "reduce_all", v1=[])
+@tf_export("reduce_all", "math.reduce_all", v1=[])
@dispatch.add_dispatch_support
def reduce_all(input_tensor, axis=None, keepdims=False, name=None):
"""Computes the "logical and" of elements across dimensions of a tensor.
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -3034,7 +2708,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_any", "reduce_any"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -3048,8 +2721,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -3095,8 +2768,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` is None, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -3134,7 +2807,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(v1=["math.reduce_logsumexp", "reduce_logsumexp"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_args(None,
"keep_dims is deprecated, use keepdims instead",
"keep_dims")
@@ -3148,8 +2820,8 @@
Source code for tensorflow.python.ops.math_ops
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` has no entries, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -3191,14 +2863,13 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.reduce_logsumexp", "reduce_logsumexp", v1=[])
-@dispatch.add_dispatch_support
def reduce_logsumexp(input_tensor, axis=None, keepdims=False, name=None):
"""Computes log(sum(exp(elements across dimensions of a tensor))).
Reduces `input_tensor` along the dimensions given in `axis`.
Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
- of the entries in `axis`, which must be unique. If `keepdims` is true, the
- reduced dimensions are retained with length 1.
+ entry in `axis`. If `keepdims` is true, the reduced dimensions
+ are retained with length 1.
If `axis` has no entries, all dimensions are reduced, and a
tensor with a single element is returned.
@@ -3247,12 +2918,11 @@
Source code for tensorflow.python.ops.math_ops
dims=reduce_dim))
if not keepdims:
my_max = array_ops.reshape(my_max, gen_array_ops.shape(result))
- result = _add_dispatch(result, my_max, name=name)
+ result = gen_math_ops.add(result, my_max)
return _may_reduce_to_scalar(keepdims, axis, result)
@tf_export("linalg.trace", v1=["linalg.trace", "trace"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("trace")
@dispatch.add_dispatch_support
def trace(x, name=None):
@@ -3262,7 +2932,7 @@
Source code for tensorflow.python.ops.math_ops
in x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output
is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where
- `output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])`
+ `output[i, j, k, ..., l] = trace(x[i, j, i, ..., l, :, :])`
For example:
@@ -3393,12 +3063,12 @@
Source code for tensorflow.python.ops.math_ops
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero.
See `tf.sparse.sparse_dense_matmul`
- for some support for `tf.sparse.SparseTensor` multiplication.
+ for some support for `tf.SparseTensor` multiplication.
b_is_sparse: If `True`, `b` is treated as a sparse matrix. Notice, this
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero.
See `tf.sparse.sparse_dense_matmul`
- for some support for `tf.sparse.SparseTensor` multiplication.
+ for some support for `tf.SparseTensor` multiplication.
name: Name for the operation (optional).
Returns:
@@ -3426,10 +3096,10 @@
Source code for tensorflow.python.ops.math_ops
if not isinstance(a, (ops.EagerTensor, _resource_variable_type)):
a = ops.convert_to_tensor(a, name="a")
if not isinstance(b, (ops.EagerTensor, _resource_variable_type)):
- b = ops.convert_to_tensor(b, dtype_hint=a.dtype.base_dtype, name="b")
+ b = ops.convert_to_tensor(b, name="b")
else:
a = ops.convert_to_tensor(a, name="a")
- b = ops.convert_to_tensor(b, dtype_hint=a.dtype.base_dtype, name="b")
+ b = ops.convert_to_tensor(b, name="b")
# TODO(apassos) remove _shape_tuple here when it is not needed.
a_shape = a._shape_tuple() # pylint: disable=protected-access
@@ -3492,7 +3162,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("linalg.matvec")
-@dispatch.add_dispatch_support
def matvec(a,
b,
transpose_a=False,
@@ -3596,7 +3265,6 @@
Source code for tensorflow.python.ops.math_ops
sparse_matmul = deprecation.deprecated(None, "Use `tf.linalg.matmul` instead")(
gen_math_ops.sparse_mat_mul)
tf_export(v1=["sparse_matmul"])(sparse_matmul)
-@dispatch.add_dispatch_support
@ops.RegisterStatistics("MatMul", "flops")
@@ -3729,12 +3397,12 @@
Source code for tensorflow.python.ops.math_ops
ValueError: If `inputs` don't all have same shape and dtype or the shape
cannot be inferred.
"""
- if not inputs or not isinstance(inputs, collections_abc.Iterable):
- raise ValueError("inputs must be an iterable of at least one "
+ if not inputs or not isinstance(inputs, (list, tuple)):
+ raise ValueError("inputs must be a list of at least one "
"Tensor/IndexedSlices with the same dtype and shape")
inputs = ops.convert_n_to_tensor_or_indexed_slices(inputs)
if not all(isinstance(x, (ops.Tensor, ops.IndexedSlices)) for x in inputs):
- raise ValueError("inputs must be an iterable of at least one "
+ raise ValueError("inputs must be a list of at least one "
"Tensor/IndexedSlices with the same dtype and shape")
if len(inputs) == 1:
@@ -3749,7 +3417,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.accumulate_n", v1=["math.accumulate_n", "accumulate_n"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("accumulate_n")
def accumulate_n(inputs, shape=None, tensor_dtype=None, name=None):
"""Returns the element-wise sum of a list of tensors.
@@ -3828,13 +3495,12 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.sigmoid", "nn.sigmoid", "sigmoid")
-@dispatch.add_dispatch_support
def sigmoid(x, name=None):
r"""Computes sigmoid of `x` element-wise.
- Formula for calculating $\mathrm{sigmoid}(x) = y = 1 / (1 + \exp(-x))$.
+ Formula for calculating sigmoid(x): `y = 1 / (1 + exp(-x))`.
- For $x \in (-\infty, \infty)$, $\mathrm{sigmoid}(x) \in (0, 1)$.
+ For x \in (-inf, inf) => sigmoid(x) \in (0, 1)
Example Usage:
@@ -3862,9 +3528,9 @@
Source code for tensorflow.python.ops.math_ops
Returns:
A Tensor with the same type as `x`.
-
+
Usage Example:
-
+
>>> x = tf.constant([-128.0, 0.0, 128.0], dtype=tf.float32)
>>> tf.sigmoid(x)
<tf.Tensor: shape=(3,), dtype=float32,
@@ -3894,37 +3560,121 @@
Source code for tensorflow.python.ops.math_ops
Returns:
A Tensor with the same type as `x`.
+ """
+ with ops.name_scope(name, "LogSigmoid", [x]) as name:
+ x = ops.convert_to_tensor(x, name="x")
+ return gen_math_ops.neg(gen_nn_ops.softplus(-x), name=name)
- Usage Example:
- If a positive number is large, then its log_sigmoid will approach to 0 since
- the formula will be `y = log( <large_num> / (1 + <large_num>) )` which
- approximates to `log (1)` which is 0.
+@tf_export("math.bincount", v1=[])
+def bincount(arr,
+ weights=None,
+ minlength=None,
+ maxlength=None,
+ dtype=dtypes.int32,
+ name=None):
+ """Counts the number of occurrences of each value in an integer array.
- >>> x = tf.constant([0.0, 1.0, 50.0, 100.0])
- >>> tf.math.log_sigmoid(x)
- <tf.Tensor: shape=(4,), dtype=float32, numpy=
- array([-6.9314718e-01, -3.1326169e-01, -1.9287499e-22, -0.0000000e+00],
- dtype=float32)>
+ If `minlength` and `maxlength` are not given, returns a vector with length
+ `tf.reduce_max(arr) + 1` if `arr` is non-empty, and length 0 otherwise.
+ If `weights` are non-None, then index `i` of the output stores the sum of the
+ value in `weights` at each index where the corresponding value in `arr` is
+ `i`.
- If a negative number is large, its log_sigmoid will approach to the number
- itself since the formula will be `y = log( 1 / (1 + <large_num>) )` which is
- `log (1) - log ( (1 + <large_num>) )` which approximates to `- <large_num>`
- that is the number itself.
+ ```python
+ values = tf.constant([1,1,2,3,2,4,4,5])
+ tf.math.bincount(values) #[0 2 2 1 2 1]
+ ```
+ Vector length = Maximum element in vector `values` is 5. Adding 1, which is 6
+ will be the vector length.
+
+ Each bin value in the output indicates number of occurrences of the particular
+ index. Here, index 1 in output has a value 2. This indicates value 1 occurs
+ two times in `values`.
+
+ ```python
+ values = tf.constant([1,1,2,3,2,4,4,5])
+ weights = tf.constant([1,5,0,1,0,5,4,5])
+ tf.math.bincount(values, weights=weights) #[0 6 0 1 9 5]
+ ```
+ Bin will be incremented by the corresponding weight instead of 1.
+ Here, index 1 in output has a value 6. This is the summation of weights
+ corresponding to the value in `values`.
+
+ Args:
+ arr: An int32 tensor of non-negative values.
+ weights: If non-None, must be the same shape as arr. For each value in
+ `arr`, the bin will be incremented by the corresponding weight instead of
+ 1.
+ minlength: If given, ensures the output has length at least `minlength`,
+ padding with zeros at the end if necessary.
+ maxlength: If given, skips values in `arr` that are equal or greater than
+ `maxlength`, ensuring that the output has length at most `maxlength`.
+ dtype: If `weights` is None, determines the type of the output bins.
+ name: A name scope for the associated operations (optional).
+
+ Returns:
+ A vector with the same dtype as `weights` or the given `dtype`. The bin
+ values.
+
+ Raises:
+ `InvalidArgumentError` if negative values are provided as an input.
- >>> x = tf.constant([-100.0, -50.0, -1.0, 0.0])
- >>> tf.math.log_sigmoid(x)
- <tf.Tensor: shape=(4,), dtype=float32, numpy=
- array([-100. , -50. , -1.3132616, -0.6931472],
- dtype=float32)>
"""
- with ops.name_scope(name, "LogSigmoid", [x]) as name:
- x = ops.convert_to_tensor(x, name="x")
- return gen_math_ops.neg(gen_nn_ops.softplus(-x), name=name)
+ name = "bincount" if name is None else name
+ with ops.name_scope(name):
+ arr = ops.convert_to_tensor(arr, name="arr", dtype=dtypes.int32)
+ array_is_nonempty = reduce_prod(array_ops.shape(arr)) > 0
+ output_size = cast(array_is_nonempty, dtypes.int32) * (reduce_max(arr) + 1)
+ if minlength is not None:
+ minlength = ops.convert_to_tensor(
+ minlength, name="minlength", dtype=dtypes.int32)
+ output_size = gen_math_ops.maximum(minlength, output_size)
+ if maxlength is not None:
+ maxlength = ops.convert_to_tensor(
+ maxlength, name="maxlength", dtype=dtypes.int32)
+ output_size = gen_math_ops.minimum(maxlength, output_size)
+ if weights is not None:
+ weights = ops.convert_to_tensor(weights, name="weights")
+ return gen_math_ops.unsorted_segment_sum(weights, arr, output_size)
+ weights = constant_op.constant([], dtype)
+ return gen_math_ops.bincount(arr, output_size, weights)
+
+
+@tf_export(v1=["math.bincount", "bincount"])
+@deprecation.deprecated_endpoints("bincount")
+def bincount_v1(arr,
+ weights=None,
+ minlength=None,
+ maxlength=None,
+ dtype=dtypes.int32):
+ """Counts the number of occurrences of each value in an integer array.
+
+ If `minlength` and `maxlength` are not given, returns a vector with length
+ `tf.reduce_max(arr) + 1` if `arr` is non-empty, and length 0 otherwise.
+ If `weights` are non-None, then index `i` of the output stores the sum of the
+ value in `weights` at each index where the corresponding value in `arr` is
+ `i`.
+
+ Args:
+ arr: An int32 tensor of non-negative values.
+ weights: If non-None, must be the same shape as arr. For each value in
+ `arr`, the bin will be incremented by the corresponding weight instead of
+ 1.
+ minlength: If given, ensures the output has length at least `minlength`,
+ padding with zeros at the end if necessary.
+ maxlength: If given, skips values in `arr` that are equal or greater than
+ `maxlength`, ensuring that the output has length at most `maxlength`.
+ dtype: If `weights` is None, determines the type of the output bins.
+
+ Returns:
+ A vector with the same dtype as `weights` or the given `dtype`. The bin
+ values.
+ """
+ return bincount(arr, weights, minlength, maxlength, dtype)
@tf_export("math.cumsum", "cumsum")
-@dispatch.add_dispatch_support
def cumsum(x, axis=0, exclusive=False, reverse=False, name=None):
"""Compute the cumulative sum of the tensor `x` along `axis`.
@@ -3996,7 +3746,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.cumprod", v1=["math.cumprod", "cumprod"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("cumprod")
def cumprod(x, axis=0, exclusive=False, reverse=False, name=None):
"""Compute the cumulative product of the tensor `x` along `axis`.
@@ -4050,7 +3799,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.cumulative_logsumexp", v1=["math.cumulative_logsumexp"])
-@dispatch.add_dispatch_support
def cumulative_logsumexp(x, axis=0, exclusive=False, reverse=False, name=None):
"""Compute the cumulative log-sum-exp of the tensor `x` along `axis`.
@@ -4111,29 +3859,20 @@
Source code for tensorflow.python.ops.math_ops
def conj(x, name=None):
r"""Returns the complex conjugate of a complex number.
- Given a tensor `x` of complex numbers, this operation returns a tensor of
- complex numbers that are the complex conjugate of each element in `x`. The
- complex numbers in `x` must be of the form \\(a + bj\\), where `a` is the
- real part and `b` is the imaginary part.
+ Given a tensor `input` of complex numbers, this operation returns a tensor of
+ complex numbers that are the complex conjugate of each element in `input`. The
+ complex numbers in `input` must be of the form \\(a + bj\\), where *a* is the
+ real part and *b* is the imaginary part.
The complex conjugate returned by this operation is of the form \\(a - bj\\).
For example:
- >>> x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j])
- >>> tf.math.conj(x)
- <tf.Tensor: shape=(2,), dtype=complex128,
- numpy=array([-2.25-4.75j, 3.25-5.75j])>
+ # tensor 'input' is [-2.25 + 4.75j, 3.25 + 5.75j]
+ tf.math.conj(input) ==> [-2.25 - 4.75j, 3.25 - 5.75j]
If `x` is real, it is returned unchanged.
- For example:
-
- >>> x = tf.constant([-2.25, 3.25])
- >>> tf.math.conj(x)
- <tf.Tensor: shape=(2,), dtype=float32,
- numpy=array([-2.25, 3.25], dtype=float32)>
-
Args:
x: `Tensor` to conjugate. Must have numeric or variant type.
name: A name for the operation (optional).
@@ -4143,10 +3882,6 @@
Source code for tensorflow.python.ops.math_ops
Raises:
TypeError: If `x` is not a numeric tensor.
-
- @compatibility(numpy)
- Equivalent to numpy.conj.
- @end_compatibility
"""
if isinstance(x, ops.Tensor):
dt = x.dtype
@@ -4173,18 +3908,11 @@
Source code for tensorflow.python.ops.math_ops
Returns:
A 1-D Tensor, the output shape as if keepdims were set to True.
"""
- # TODO(allenl): Refactor `reduced_shape` to take the tensor corresponding to
- # `input_shape` rather than `tf.shape` of it. Then we can check if the shape
- # is fully defined here, which may be faster executing eagerly than running
- # `tf.shape` and then fetching its constant value.
- constant_input_shape = tensor_util.constant_value(input_shape)
- if constant_input_shape is not None:
- constant_axes = tensor_util.constant_value(axes)
- if constant_axes is not None:
- constant_axes = np.array(constant_axes, dtype=np.int32)
- constant_input_shape = np.array(constant_input_shape, dtype=np.int32)
- constant_input_shape[constant_axes] = 1
- return constant_input_shape
+ if context.executing_eagerly():
+ input_shape = input_shape.numpy()
+ axes = axes.numpy()
+ input_shape[axes] = 1
+ return input_shape
# Example:
# cast needed for SparseTensor reductions
@@ -4230,7 +3958,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(
"math.unsorted_segment_mean",
v1=["math.unsorted_segment_mean", "unsorted_segment_mean"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("unsorted_segment_mean")
@dispatch.add_dispatch_support
def unsorted_segment_mean(data, segment_ids, num_segments, name=None):
@@ -4240,7 +3967,8 @@
Source code for tensorflow.python.ops.math_ops
segmentation](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/math#about_segmentation)
for an explanation of segments.
- This operator is similar to the `tf.math.unsorted_segment_sum` operator.
+ This operator is similar to the unsorted segment sum operator found
+ [here](../../../api_docs/python/math_ops.md#UnsortedSegmentSum).
Instead of computing the sum over segments, it computes the mean of all
entries belonging to a segment such that:
@@ -4276,7 +4004,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export(
"math.unsorted_segment_sqrt_n",
v1=["math.unsorted_segment_sqrt_n", "unsorted_segment_sqrt_n"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("unsorted_segment_sqrt_n")
@dispatch.add_dispatch_support
def unsorted_segment_sqrt_n(data, segment_ids, num_segments, name=None):
@@ -4286,7 +4013,8 @@
Source code for tensorflow.python.ops.math_ops
segmentation](https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/math#about_segmentation)
for an explanation of segments.
- This operator is similar to the `tf.math.unsorted_segment_sum` operator.
+ This operator is similar to the unsorted segment sum operator found
+ [here](../../../api_docs/python/math_ops.md#UnsortedSegmentSum).
Additionally to computing the sum over segments, it divides the results by
sqrt(N).
@@ -4625,7 +4353,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("tensordot", "linalg.tensordot")
-@dispatch.add_dispatch_support
def tensordot(a, b, axes, name=None):
r"""Tensor contraction of a and b along specified axes and outer product.
@@ -4738,7 +4465,7 @@
Source code for tensorflow.python.ops.math_ops
rank_a = array_ops.rank(a)
axes = ops.convert_to_tensor(axes, dtype=dtypes.int32, name="axes")
axes = array_ops.where(axes >= 0, axes, axes + rank_a)
- free, _ = gen_array_ops.list_diff(range(rank_a), axes, dtypes.int32)
+ free, _ = array_ops.setdiff1d(range(rank_a), axes)
free_dims = array_ops.gather(shape_a, free)
axes_dims = array_ops.gather(shape_a, axes)
prod_free_dims = reduce_prod(free_dims)
@@ -4812,22 +4539,21 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.polyval")
-@dispatch.add_dispatch_support
def polyval(coeffs, x, name=None):
r"""Computes the elementwise value of a polynomial.
If `x` is a tensor and `coeffs` is a list n + 1 tensors,
this function returns the value of the n-th order polynomial
- `p(x) = coeffs[n-1] + coeffs[n-2] * x + ... + coeffs[0] * x**(n-1)`
+ p(x) = coeffs[n-1] + coeffs[n-2] * x + ... + coeffs[0] * x**(n-1)
evaluated using Horner's method, i.e.
- `p(x) = coeffs[n-1] + x * (coeffs[n-2] + ... + x * (coeffs[1]
- + x * coeffs[0]))`
-
+ p(x) = coeffs[n-1] + x * (coeffs[n-2] + ... + x * (coeffs[1] +
+ x * coeffs[0]))
+
Usage Example:
-
+
>>> coefficients = [1.0, 2.5, -4.2]
>>> x = 5.0
>>> y = tf.math.polyval(coefficients, x)
@@ -4883,7 +4609,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.reciprocal_no_nan")
-@dispatch.add_dispatch_support
def reciprocal_no_nan(x, name=None):
"""Performs a safe reciprocal operation, element wise.
@@ -4985,37 +4710,7 @@
Source code for tensorflow.python.ops.math_ops
return gen_math_ops.ndtri(x)
-@tf_export("math.erfcinv")
-@dispatch.add_dispatch_support
-def erfcinv(x, name=None):
- """Computes the inverse of complementary error function.
-
- Given `x`, compute the inverse complementary error function of `x`.
- This function is the inverse of `tf.math.erfc`, and is defined on
- `[0, 2]`.
-
- >>> tf.math.erfcinv([0., 0.2, 1., 1.5, 2.])
- <tf.Tensor: shape=(5,), dtype=float32, numpy=
- array([ inf, 0.9061935, -0. , -0.4769363, -inf],
- dtype=float32)>
-
- Args:
- x: `Tensor` with type `float` or `double`.
- name: A name for the operation (optional).
- Returns:
- Inverse complementary error function of `x`.
-
- @compatibility(numpy)
- Equivalent to scipy.special.erfcinv
- @end_compatibility
- """
- with ops.name_scope(name, "erfcinv", [x]):
- x = ops.convert_to_tensor(x, name="start")
- return -ndtri(0.5 * x) * np.sqrt(0.5)
-
-
@tf_export("math.ceil", v1=["math.ceil", "ceil"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("ceil")
@dispatch.add_dispatch_support
def ceil(x, name=None):
@@ -5100,14 +4795,10 @@
Source code for tensorflow.python.ops.math_ops
numpy=array([ 7.389056, 2980.958 ], dtype=float32)>
For complex numbers, the exponential value is calculated as
- $$
- e^{x+iy} = {e^x} {e^{iy}} = {e^x} ({\cos (y) + i \sin (y)})
- $$
+ \\(e^{x+iy}={e^x}{e^{iy}}={e^x}{\\cos(y)+i\\sin(y)}\\)
For `1+1j` the value would be computed as:
- $$
- e^1 (\cos (1) + i \sin (1)) = 2.7182817 \times (0.5403023+0.84147096j)
- $$
+ \\(e^1{\\cos(1)+i\\sin(1)} = 2.7182817 \\times (0.5403023+0.84147096j)\\)
>>> x = tf.constant(1 + 1j)
>>> tf.math.exp(x)
@@ -5133,7 +4824,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.sobol_sample")
-@dispatch.add_dispatch_support
def sobol_sample(dim, num_results, skip=0, dtype=dtypes.float32, name=None):
"""Generates points from the Sobol sequence.
@@ -5158,7 +4848,6 @@
Source code for tensorflow.python.ops.math_ops
@tf_export("math.rsqrt", v1=["math.rsqrt", "rsqrt"])
-@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("rsqrt")
@dispatch.add_dispatch_support
def rsqrt(x, name=None):
@@ -5173,69 +4862,13 @@
Source code for tensorflow.python.ops.math_ops
Args:
x: A `tf.Tensor`. Must be one of the following types: `bfloat16`, `half`,
- `float32`, `float64`.
+ `float32`, `float64`. `int32`
name: A name for the operation (optional).
Returns:
A `tf.Tensor`. Has the same type as `x`.
"""
return gen_math_ops.rsqrt(x, name)
-
-
-@tf_export("math.acos", "acos")
-@dispatch.add_dispatch_support
-def acos(x, name=None):
- """Computes acos of x element-wise.
-
- Provided an input tensor, the `tf.math.acos` operation
- returns the inverse cosine of each element of the tensor.
- If `y = tf.math.cos(x)` then, `x = tf.math.acos(y)`.
-
- Input range is `[-1, 1]` and the output has a range of `[0, pi]`.
-
- For example:
-
- >>> x = tf.constant([1.0, -0.5, 3.4, 0.2, 0.0, -2], dtype = tf.float32)
- >>> tf.math.acos(x)
- <tf.Tensor: shape=(6,), dtype=float32,
- numpy= array([0. , 2.0943952, nan, 1.3694383, 1.5707964, nan],
- dtype=float32)>
-
- Args:
- x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`,
- `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`,
- `complex64`, `complex128`, `string`.
- name: A name for the operation (optional).
-
- Returns:
- A `Tensor`. Has the same type as x.
- """
- return gen_math_ops.acos(x, name)
-
-
-@tf_export("math.floor", "floor")
-@dispatch.add_dispatch_support
-def floor(x, name=None):
- """Returns element-wise largest integer not greater than x.
-
- Both input range is `(-inf, inf)` and the
- ouput range consists of all integer values.
-
- For example:
-
- >>> x = tf.constant([1.3324, -1.5, 5.555, -2.532, 0.99, float("inf")])
- >>> tf.floor(x).numpy()
- array([ 1., -2., 5., -3., 0., inf], dtype=float32)
-
- Args:
- x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`,
- `float32`, `float64`.
- name: A name for the operation (optional).
-
- Returns:
- A `Tensor`. Has the same type as x.
- """
- return gen_math_ops.floor(x, name)
diff --git a/sparseml/_sources/index.rst.txt b/sparseml/_sources/index.rst.txt
index 2f48185854f..80e266e9277 100644
--- a/sparseml/_sources/index.rst.txt
+++ b/sparseml/_sources/index.rst.txt
@@ -17,8 +17,7 @@
SparseML |version|
===================
-Libraries for state-of-the-art deep neural network optimization algorithms,
-enabling simple pipelines integration with a few lines of code
+Libraries for applying sparsification recipes to neural networks with a few lines of code, enabling faster and smaller models
.. raw:: html
@@ -49,45 +48,50 @@ enabling simple pipelines integration with a few lines of code
Overview
========
-SparseML is a toolkit that includes APIs, CLIs, scripts and libraries that apply state-of-the-art optimization
-algorithms such as `pruning `_ and
-`quantization `_ to any neural network.
-General, recipe-driven approaches built around these optimizations enable the simplification of creating faster
-and smaller models for the ML performance community at large.
+SparseML is a toolkit that includes APIs, CLIs, scripts and libraries that apply state-of-the-art sparsification algorithms such as pruning and quantization to any neural network.
+General, recipe-driven approaches built around these algorithms enable the simplification of creating faster and smaller models for the ML performance community at large.
-SparseML is integrated for easy model optimizations within the `PyTorch `_,
-`Keras `_, and `TensorFlow V1 `_ ecosystems currently.
+`This repository `_ contains integrations within the `PyTorch `_, `Keras `_, and `TensorFlow V1 `_, allowing for seamless model sparsification.
-Related Products
-================
+Sparsification
+==============
-- `DeepSparse `_:
- CPU inference engine that delivers unprecedented performance for sparse models
-- `SparseZoo `_:
- Neural network model repository for highly sparse models and optimization recipes
-- `Sparsify `_:
- Easy-to-use autoML interface to optimize deep neural networks for
- better inference performance and a smaller footprint
+Sparsification is the process of taking a trained deep learning model and removing redundant information from the overprecise and over-parameterized network resulting in a faster and smaller model.
+Techniques for sparsification are all encompassing including everything from inducing sparsity using `pruning `_ and `quantization `_ to enabling naturally occurring sparsity using `activation sparsity `_ or `winograd/FFT `_.
+When implemented correctly, these techniques result in significantly more performant and smaller models with limited to no effect on the baseline metrics.
+For example, pruning plus quantization can give over `7x improvements in performance `_ while recovering to nearly the same baseline accuracy.
+
+The Deep Sparse product suite builds on top of sparsification enabling you to easily apply the techniques to your datasets and models using recipe-driven approaches.
+Recipes encode the directions for how to sparsify a model into a simple, easily editable format.
+- Download a sparsification recipe and sparsified model from the `SparseZoo `_.
+- Alternatively, create a recipe for your model using `Sparsify `_.
+- Apply your recipe with only a few lines of code using `SparseML `_.
+- Finally, for GPU-level performance on CPUs, deploy your sparse-quantized model with the `DeepSparse Engine `_.
+
+
+**Full Deep Sparse product flow:**
+
+
Resources and Learning More
===========================
-- `SparseZoo Documentation `_
-- `Sparsify Documentation `_
-- `DeepSparse Documentation `_
-- `Neural Magic Blog `_,
- `Resources `_,
- `Website `_
+- `SparseZoo Documentation `_
+- `Sparsify Documentation `_
+- `DeepSparse Documentation `_
+- `Neural Magic Blog `_,
+ `Resources `_,
+ `Website `_
Release History
===============
Official builds are hosted on PyPi
-- stable: `sparseml `_
-- nightly (dev): `sparseml-nightly `_
+- stable: `sparseml `_
+- nightly (dev): `sparseml-nightly `_
Additionally, more information can be found via
-`GitHub Releases `_.
+`GitHub Releases `_.
.. toctree::
:maxdepth: 3
@@ -104,8 +108,9 @@ Additionally, more information can be found via
api/sparseml
.. toctree::
- :maxdepth: 2
- :caption: Help and Support
+ :maxdepth: 3
+ :caption: Help
Bugs, Feature Requests
- Support, General Q&A
\ No newline at end of file
+ Support, General Q&A
+ Neural Magic Docs
diff --git a/sparseml/_sources/installation.md.txt b/sparseml/_sources/installation.md.txt
index 397046cf16e..5016202241b 100644
--- a/sparseml/_sources/installation.md.txt
+++ b/sparseml/_sources/installation.md.txt
@@ -25,4 +25,4 @@ Install with pip using:
pip install sparseml
```
-Then if you would like to explore any of the [scripts](https://github.com/neuralmagic/sparseml/tree/main/scripts), [notebooks](https://github.com/neuralmagic/sparseml/tree/main/notebooks), or [examples](https://github.com/neuralmagic/sparseml/tree/main/examples) clone the repository and install any additional dependencies as required.
+Then if you would like to explore any of the [scripts](https://github.com/neuralmagic/sparseml/tree/main/scripts), [notebooks](https://github.com/neuralmagic/sparseml/tree/main/notebooks), or [integrations](https://github.com/neuralmagic/sparseml/tree/main/integrations) clone the repository and install any additional dependencies as required.
diff --git a/sparseml/_sources/quicktour.md.txt b/sparseml/_sources/quicktour.md.txt
index 380fc0cf803..eaf4bdfc9c0 100644
--- a/sparseml/_sources/quicktour.md.txt
+++ b/sparseml/_sources/quicktour.md.txt
@@ -16,12 +16,11 @@ limitations under the License.
## Quick Tour
-To enable flexibility, ease of use, and repeatability, optimizing a model is generally done using a recipe file.
-The files encode the instructions needed for modifying the model and/or training process as a list of modifiers.
+To enable flexibility, ease of use, and repeatability, sparsifying a model is generally done using a recipe.
+The recipes encode the instructions needed for modifying the model and/or training process as a list of modifiers.
Example modifiers can be anything from setting the learning rate for the optimizer to gradual magnitude pruning.
The files are written in [YAML](https://yaml.org/) and stored in YAML or [markdown](https://www.markdownguide.org/) files using [YAML front matter](https://assemble.io/docs/YAML-front-matter.html).
-The rest of the SparseML system is coded to parse the recipe files into a native format for the desired framework
-and apply the modifications to the model and training pipeline.
+The rest of the SparseML system is coded to parse the recipes into a native format for the desired framework and apply the modifications to the model and training pipeline.
A sample recipe for pruning a model generally looks like the following:
@@ -56,12 +55,15 @@ Pre-configured recipes and the resulting models can be explored and downloaded f
For a more in-depth read, check out [SparseML documentation](https://docs.neuralmagic.com/sparseml/).
-### PyTorch Optimization
+### PyTorch Sparsification
-The PyTorch optimization libraries are located under the `sparseml.pytorch.optim` package.
-Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into PyTorch training pipelines.
+The PyTorch sparsification libraries are located under the `sparseml.pytorch.optim` package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into PyTorch training pipelines.
-The integration is done using the `ScheduledOptimizer` class. It is intended to wrap your current optimizer and its step function. The step function then calls into the `ScheduledModifierManager` class which can be created from a recipe file. With this setup, the training process can then be modified as desired to optimize the model.
+The integration is done using the `ScheduledOptimizer` class.
+It is intended to wrap your current optimizer and its step function.
+The step function then calls into the `ScheduledModifierManager` class which can be created from a recipe file.
+With this setup, the training process can then be modified as desired to sparsify the model.
To enable all of this, the integration code you'll need to write is only a handful of lines:
@@ -80,11 +82,11 @@ optimizer = ScheduledOptimizer(optimizer, model, manager, steps_per_epoch=num_tr
### Keras Optimization
-The Keras optimization libraries are located under the `sparseml.keras.optim` package.
-Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into Keras training pipelines.
+The Keras sparsification libraries are located under the `sparseml.keras.optim` package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into Keras training pipelines.
The integration is done using the `ScheduledModifierManager` class which can be created from a recipe file.
-This class handles modifying the Keras objects for the desired optimizations using the `modify` method.
+This class handles modifying the Keras objects for the desired algorithms using the `modify` method.
The edited model, optimizer, and any callbacks necessary to modify the training process are returned.
The model and optimizer can be used normally and the callbacks must be passed into the `fit` or `fit_generator` function.
If using `train_on_batch`, the callbacks must be invoked after each call.
@@ -114,15 +116,16 @@ model.fit(..., callbacks=callbacks)
save_model = manager.finalize(model)
```
-### TensorFlow V1 Optimization
+### TensorFlow V1 Sparsification
-The TensorFlow optimization libraries for TensorFlow version 1.X are located under the `sparseml.tensorflow_v1.optim` package. Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into TensorFlow V1 training pipelines.
+The TensorFlow sparsification libraries for TensorFlow version 1.X are located under the `sparseml.tensorflow_v1.optim` package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into TensorFlow V1 training pipelines.
The integration is done using the `ScheduledModifierManager` class which can be created from a recipe file.
-This class handles modifying the TensorFlow graph for the desired optimizations.
-With this setup, the training process can then be modified as desired to optimize the model.
+This class handles modifying the TensorFlow graph for the desired algorithms.
+With this setup, the training process can then be modified as desired to sparsify the model.
-#### Estimator-based pipelines
+#### Estimator-Based pipelines
Estimator-based pipelines are simpler to integrate with as compared to session-based pipelines.
The `ScheduledModifierManager` can override the necessary callbacks in the estimator to modify the graph using the `modify_estimator` function.
@@ -139,12 +142,12 @@ manager.modify_estimator(estimator, steps_per_epoch=num_train_batches)
# Normal estimator training code...
```
-#### Session-based pipelines
+#### Session-Based pipelines
Session-based pipelines need a little bit more as compared to estimator-based pipelines; however,
it is still designed to require only a few lines of code for integration.
After graph creation, the manager's `create_ops` method must be called.
-This will modify the graph as needed for the optimizations and return modifying ops and extras.
+This will modify the graph as needed for the algorithms and return modifying ops and extras.
After creating the session and training normally, call into `session.run` with the modifying ops after each step.
Modifying extras contain objects such as tensorboard summaries of the modifiers to be used if desired.
Finally, once completed, `complete_graph` must be called to remove the modifying ops for saving and export.
@@ -235,3 +238,4 @@ with tf_compat.Graph().as_default() as graph:
exporter.export_pb(outputs=[logits])
exporter.export_onnx(inputs=input_names, outputs=output_names)
+```
diff --git a/sparseml/_sources/recipes.md.txt b/sparseml/_sources/recipes.md.txt
index b5120953c00..b47e16fef11 100644
--- a/sparseml/_sources/recipes.md.txt
+++ b/sparseml/_sources/recipes.md.txt
@@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
-# Optimization Recipes
+# Sparsification Recipes
-All optimization APIs are designed to work with recipe files.
+All SparseML Sparsification APIs are designed to work with recipes.
The files encode the instructions needed for modifying the model and/or training process as a list of modifiers.
Example modifiers can be anything from setting the learning rate for the optimizer to gradual magnitude pruning.
The files are written in [YAML](https://yaml.org/) and stored in YAML or
@@ -27,12 +27,10 @@ and apply the modifications to the model and training pipeline.
In a recipe, modifiers must be written in a list that includes "modifiers" in its name.
-The easiest ways to get or create optimization recipes are by either using
-the pre-configured recipes in [SparseZoo](https://github.com/neuralmagic/sparsezoo) or
-using [Sparsify's](https://github.com/neuralmagic/sparsify) autoML style creation.
+The easiest ways to get or create recipes are by either using the pre-configured recipes in [SparseZoo](https://github.com/neuralmagic/sparsezoo) or using [Sparsify's](https://github.com/neuralmagic/sparsify) automatic creation.
However, power users may be inclined to create their own recipes by hand to enable more
-fine grained control or to add in custom modifiers.
+fine-grained control or to add in custom modifiers.
A sample recipe for pruning a model generally looks like the following:
```yaml
@@ -185,7 +183,7 @@ Notes:
the script `scripts/pytorch/model_quantize_qat_export.py` or the function
`neuralmagicML.pytorch.quantization.quantize_qat_export`.
- If performing QAT on a sparse model, you must preserve sparsity during QAT by
- applying a `ConstantKSModifier` or have already used a `GradualKSModifier` with
+ applying a `ConstantPruningModifier` or have already used a `GMPruningModifier` with
`leave_enabled` set to True.
Required Parameters:
diff --git a/sparseml/api/modules.html b/sparseml/api/modules.html
index 39abc166b45..924bbc9482f 100644
--- a/sparseml/api/modules.html
+++ b/sparseml/api/modules.html
@@ -98,16 +98,17 @@
-
+
diff --git a/sparseml/api/sparseml.html b/sparseml/api/sparseml.html
index f98a9700eb0..097ba0c7771 100644
--- a/sparseml/api/sparseml.html
+++ b/sparseml/api/sparseml.html
@@ -44,7 +44,7 @@
-
+
@@ -100,7 +100,7 @@
@@ -120,10 +120,11 @@
-
+
@@ -467,7 +468,7 @@ Submodules
Next
- Previous
+ Previous
diff --git a/sparseml/api/sparseml.keras.html b/sparseml/api/sparseml.keras.html
index c6fa41d839c..7cbf4ece718 100644
--- a/sparseml/api/sparseml.keras.html
+++ b/sparseml/api/sparseml.keras.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.keras.optim.html b/sparseml/api/sparseml.keras.optim.html
index e8aeb69ba8f..c454ff1741b 100644
--- a/sparseml/api/sparseml.keras.optim.html
+++ b/sparseml/api/sparseml.keras.optim.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
@@ -231,19 +232,24 @@ Submodules
-static from_yaml
(file_path: str, add_modifiers: Optional[List[sparseml.keras.optim.modifier.Modifier]] = None)[source]¶
-Convenience function used to create the manager of multiple modifiers
-from a yaml file.
+static from_yaml
(file_path: Union[str, sparsezoo.objects.optimization_recipe.OptimizationRecipe], add_modifiers: Optional[List[sparseml.keras.optim.modifier.Modifier]] = None)[source]¶
+Convenience function used to create the manager of multiple modifiers from a
+recipe file.
- Parameters
-file_path – the path to the yaml file to load the modifier from
+file_path – the path to the recipe file to load the modifier from, or
+a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+SparseZoo stubs should be preceded by ‘zoo:’, and can contain an optional
+‘?recipe_type=<type>’ parameter. Can also be a SparseZoo OptimizationRecipe
+object. i.e. ‘/path/to/local/recipe.yaml’, ‘zoo:model/stub/path’,
+‘zoo:model/stub/path?recipe_type=transfer’
add_modifiers – additional modifiers that should be added to the
-returned manager alongside the ones loaded from the yaml file
+returned manager alongside the ones loaded from the recipe file
- Returns
-ScheduledModifierManager() created from the yaml file
+ScheduledModifierManager() created from the recipe file
@@ -289,6 +295,23 @@ Submodules
+
+build
(input_shape)[source]¶
+Creates the variables of the layer (optional, for subclass implementers).
+This is a method that implementers of subclasses of Layer or Model
+can override if they need a state-creation step in-between
+layer instantiation and layer call.
+This is typically used to create the weights of Layer subclasses.
+
+- Parameters
+input_shape – Instance of TensorShape, or list of instances of
+TensorShape if the layer expects a list of inputs
+(one instance per input).
+
+
+
+
-
call
(inputs: tensorflow.python.framework.ops.Tensor, training=None)[source]¶
@@ -315,6 +338,35 @@ Submodules
+-
+classmethod
from_config
(config)[source]¶
+Creates a layer from its config.
+This method is the reverse of get_config,
+capable of instantiating the same layer from the config
+dictionary. It does not handle layer connectivity
+(handled by Network), nor weights (handled by set_weights).
+
+- Parameters
+config – A Python dictionary, typically the
+output of get_config.
+
+- Returns
+A layer instance.
+
+
+
+
+
+-
+
get_config
()[source]¶
+Get layer config
+Serialization and deserialization should be done using
+tf.keras.serialize/deserialize, which create and retrieve the “class_name”
+field automatically.
+The resulting config below therefore does not contain the field.
+
+
-
property
global_step
¶
@@ -325,6 +377,11 @@ Submodulesproperty mask_updater
¶
+
+-
+property
masked_layer
¶
+
+
-
property
masks
¶
@@ -347,6 +404,26 @@ Submodulesclass sparseml.keras.optim.mask_pruning.
PruningScheduler
[source]¶
Bases: abc.ABC
Abstract pruning scheduler
+
+-
+classmethod
deserialize
(config)[source]¶
+Deserialize a pruning scheduler from config returned by scheduler’s
+get_config method
+
+- Parameters
+config – a pruning scheduler’s config
+
+- Returns
+a pruning scheduler instance
+
+
+
+
+
+
-
abstract
should_prune
(step: int) → bool[source]¶
@@ -1191,7 +1268,7 @@ Submodules
-
class
sparseml.keras.optim.modifier_pruning.
ConstantPruningModifier
(params: Union[str, List[str]], start_epoch: float = - 1, end_epoch: float = - 1, log_types: Union[str, List[str]] = '__ALL__')[source]¶
-Bases: sparseml.keras.optim.modifier.ScheduledModifier
, sparseml.keras.optim.mask_pruning.PruningScheduler
+Bases: sparseml.keras.optim.modifier.ScheduledModifier
Holds the sparsity level and shape for a given param constant while training.
Useful for transfer learning use cases.
@@ -1306,7 +1383,7 @@ Submodules
-
-class
sparseml.keras.optim.modifier_pruning.
GMPruningModifier
(params: Union[str, List[str]], init_sparsity: float, final_sparsity: float, start_epoch: float, end_epoch: float, update_frequency: float, inter_func: str = 'cubic', log_types: Union[str, List[str]] = '__ALL__', mask_type: Union[str, List[int], sparseml.keras.optim.mask_pruning_creator.PruningMaskCreator] = 'unstructured', leave_enabled: bool = True)[source]¶
+class sparseml.keras.optim.modifier_pruning.
GMPruningModifier
(params: Union[str, List[str]], init_sparsity: float, final_sparsity: float, start_epoch: float, end_epoch: float, update_frequency: float, inter_func: str = 'cubic', log_types: Union[str, List[str]] = '__ALL__', mask_type: Union[str, List[int]] = 'unstructured', leave_enabled: bool = True)[source]¶
Bases: sparseml.keras.optim.modifier.ScheduledUpdateModifier
Gradually applies kernel sparsity to a given variable or variables from
init_sparsity until final_sparsity is reached over a given amount of time and
@@ -1352,7 +1429,7 @@
Submodules
-
mask_type
¶
-the PruningMaskCreator object used
+the mask type used
- Type
return
diff --git a/sparseml/api/sparseml.keras.utils.html b/sparseml/api/sparseml.keras.utils.html
index 2b73431b1b9..73318a24f58 100644
--- a/sparseml/api/sparseml.keras.utils.html
+++ b/sparseml/api/sparseml.keras.utils.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.onnx.html b/sparseml/api/sparseml.onnx.html
index 9b63151424c..a2f3565338d 100644
--- a/sparseml/api/sparseml.onnx.html
+++ b/sparseml/api/sparseml.onnx.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.onnx.optim.html b/sparseml/api/sparseml.onnx.optim.html
index 1a2bc3e58b2..0475121d042 100644
--- a/sparseml/api/sparseml.onnx.optim.html
+++ b/sparseml/api/sparseml.onnx.optim.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.onnx.optim.quantization.html b/sparseml/api/sparseml.onnx.optim.quantization.html
index c20842e56a7..560f13e9f95 100644
--- a/sparseml/api/sparseml.onnx.optim.quantization.html
+++ b/sparseml/api/sparseml.onnx.optim.quantization.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.onnx.utils.html b/sparseml/api/sparseml.onnx.utils.html
index 49c874f5e12..857f35517e8 100644
--- a/sparseml/api/sparseml.onnx.utils.html
+++ b/sparseml/api/sparseml.onnx.utils.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.optim.html b/sparseml/api/sparseml.optim.html
index d00f5b98b81..e9d3d3e46bf 100644
--- a/sparseml/api/sparseml.optim.html
+++ b/sparseml/api/sparseml.optim.html
@@ -100,7 +100,7 @@
@@ -129,10 +129,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.datasets.classification.html b/sparseml/api/sparseml.pytorch.datasets.classification.html
index 097bf3ed5fd..b89e94aacc7 100644
--- a/sparseml/api/sparseml.pytorch.datasets.classification.html
+++ b/sparseml/api/sparseml.pytorch.datasets.classification.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.datasets.detection.html b/sparseml/api/sparseml.pytorch.datasets.detection.html
index 62667e0404c..a2d50fad52f 100644
--- a/sparseml/api/sparseml.pytorch.datasets.detection.html
+++ b/sparseml/api/sparseml.pytorch.datasets.detection.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.datasets.html b/sparseml/api/sparseml.pytorch.datasets.html
index eaec8ecabad..4502c44ee55 100644
--- a/sparseml/api/sparseml.pytorch.datasets.html
+++ b/sparseml/api/sparseml.pytorch.datasets.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.datasets.recommendation.html b/sparseml/api/sparseml.pytorch.datasets.recommendation.html
index 62ac95ce4d3..a255d86ba09 100644
--- a/sparseml/api/sparseml.pytorch.datasets.recommendation.html
+++ b/sparseml/api/sparseml.pytorch.datasets.recommendation.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.datasets.video.html b/sparseml/api/sparseml.pytorch.datasets.video.html
index 57f5f1b2388..720bad9fe2e 100644
--- a/sparseml/api/sparseml.pytorch.datasets.video.html
+++ b/sparseml/api/sparseml.pytorch.datasets.video.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.html b/sparseml/api/sparseml.pytorch.html
index 628a98f118d..d168ca9a90c 100644
--- a/sparseml/api/sparseml.pytorch.html
+++ b/sparseml/api/sparseml.pytorch.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.models.classification.html b/sparseml/api/sparseml.pytorch.models.classification.html
index f4dd943675e..251fddbdbe2 100644
--- a/sparseml/api/sparseml.pytorch.models.classification.html
+++ b/sparseml/api/sparseml.pytorch.models.classification.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
@@ -302,7 +303,10 @@ SubmodulesQuick Tour
- Installation
-- Optimization Recipes
+- Sparsification Recipes
@@ -124,10 +124,11 @@
-
+
@@ -383,7 +384,10 @@ SubmodulesQuick Tour
- Installation
-- Optimization Recipes
+- Sparsification Recipes
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.models.html b/sparseml/api/sparseml.pytorch.models.html
index a460ae83761..4e8476486c6 100644
--- a/sparseml/api/sparseml.pytorch.models.html
+++ b/sparseml/api/sparseml.pytorch.models.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.models.recommendation.html b/sparseml/api/sparseml.pytorch.models.recommendation.html
index f3f65c96c43..682dd6de1f3 100644
--- a/sparseml/api/sparseml.pytorch.models.recommendation.html
+++ b/sparseml/api/sparseml.pytorch.models.recommendation.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.nn.html b/sparseml/api/sparseml.pytorch.nn.html
index b122f1cc66b..0edeeb76eff 100644
--- a/sparseml/api/sparseml.pytorch.nn.html
+++ b/sparseml/api/sparseml.pytorch.nn.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.pytorch.optim.html b/sparseml/api/sparseml.pytorch.optim.html
index 4e88b44562e..68c3074f47e 100644
--- a/sparseml/api/sparseml.pytorch.optim.html
+++ b/sparseml/api/sparseml.pytorch.optim.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
@@ -718,19 +719,24 @@ Submodules
-
-static
from_yaml
(file_path: str, add_modifiers: Optional[List[sparseml.pytorch.optim.modifier.Modifier]] = None)[source]¶
+static from_yaml
(file_path: Union[str, sparsezoo.objects.optimization_recipe.OptimizationRecipe], add_modifiers: Optional[List[sparseml.pytorch.optim.modifier.Modifier]] = None)[source]¶
Convenience function used to create the manager of multiple modifiers from a
-yaml file.
+recipe file.
- Parameters
-file_path – the path to the yaml file to load the modifier from
+file_path – the path to the recipe file to load the modifier from, or
+a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+SparseZoo stubs should be preceded by ‘zoo:’, and can contain an optional
+‘?recipe_type=<type>’ parameter. Can also be a SparseZoo OptimizationRecipe
+object. i.e. ‘/path/to/local/recipe.yaml’, ‘zoo:model/stub/path’,
+‘zoo:model/stub/path?recipe_type=transfer’
add_modifiers – additional modifiers that should be added to the
-returned manager alongside the ones loaded from the yaml file
+returned manager alongside the ones loaded from the recipe file
- Returns
-ScheduledModifierManager() created from the yaml file
+ScheduledModifierManager() created from the recipe file
@@ -2767,7 +2773,7 @@ Submodules
Sample yaml:
-!ConstantKSModifier
+!ConstantPruningModifier
start_epoch: 0.0
end_epoch: 10.0
@@ -2924,7 +2930,7 @@ Submodules
Sample yaml:
-!GradualKSModifier
+!GMPruningModifier
init_sparsity: 0.05
final_sparsity: 0.8
@@ -3151,7 +3157,7 @@ Submodules
-
-class
sparseml.pytorch.optim.modifier_quantization.
QuantizationModifier
(start_epoch: float = - 1.0, submodules: Optional[List[str]] = None, model_fuse_fn_name: Optional[str] = None, disable_quantization_observer_epoch: Union[None, float] = None, freeze_bn_stats_epoch: Union[None, float] = None, end_epoch: float = - 1)[source]¶
+class sparseml.pytorch.optim.modifier_quantization.
QuantizationModifier
(start_epoch: float = - 1.0, submodules: Optional[List[str]] = None, model_fuse_fn_name: Optional[str] = None, disable_quantization_observer_epoch: Union[None, float] = None, freeze_bn_stats_epoch: Union[None, float] = None, end_epoch: float = - 1, model_fuse_fn_kwargs: Optional[Dict[str, Any]] = None)[source]¶
Bases: sparseml.pytorch.optim.modifier.ScheduledModifier
Enables quantization aware training (QAT) for a given module or its submodules
After the start epoch, the specified module(s)’ forward pass will emulate
@@ -3186,6 +3192,8 @@
SubmodulesQuick Tour
- Installation
-- Optimization Recipes
+- Sparsification Recipes
@@ -124,10 +124,11 @@
-
+
@@ -218,7 +219,7 @@ Submodules
-
-
sparseml.pytorch.optim.quantization.helpers.
fuse_module_conv_bn_relus
(module: torch.nn.modules.module.Module, inplace: bool = True) → torch.nn.modules.module.Module[source]¶
+sparseml.pytorch.optim.quantization.helpers.
fuse_module_conv_bn_relus
(module: torch.nn.modules.module.Module, inplace: bool = True, override_bn_subclasses_forward: Union[bool, str] = True) → torch.nn.modules.module.Module[source]¶
Performs fusion of Conv2d, BatchNorm2d, and ReLU layers found in the
given module. To be fused, these layers must appear sequentially in
module.named_modules() and be in the same submodule.
@@ -230,6 +231,12 @@
Submodules
module – the module to fuse
inplace – set True to perform fusions in-place. default is True
+override_bn_subclasses_forward – if True, modules that are subclasses of
+BatchNorm2d will be modified to be BatchNorm2d but with the forward
+pass and state variables copied from the subclass. This is so these
+BN modules can pass PyTorch type checking when fusing. Can set to
+“override-only” and only parameters will be overwritten, not the
+forward pass. Default is True
- Returns
@@ -301,11 +308,12 @@ Submodules
-
-
sparseml.pytorch.optim.quantization.quantize_qat_export.
quantize_torch_qat_export
(model: onnx.onnx_ONNX_REL_1_6_ml_pb2.ModelProto, inplace: bool = True) → onnx.onnx_ONNX_REL_1_6_ml_pb2.ModelProto[source]¶
+sparseml.pytorch.optim.quantization.quantize_qat_export.
quantize_torch_qat_export
(model: Union[onnx.onnx_ONNX_REL_1_6_ml_pb2.ModelProto, str], output_file_path: Optional[str] = None, inplace: bool = True) → onnx.onnx_ONNX_REL_1_6_ml_pb2.ModelProto[source]¶
- Parameters
-model – The model to convert
+model – The model to convert, or a file path to it
+output_file_path – File path to save the converted model to
inplace – If true, does conversion of model in place. Default is true
diff --git a/sparseml/api/sparseml.pytorch.utils.html b/sparseml/api/sparseml.pytorch.utils.html
index 937ddec83ad..e61d748bf92 100644
--- a/sparseml/api/sparseml.pytorch.utils.html
+++ b/sparseml/api/sparseml.pytorch.utils.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
@@ -1879,7 +1880,10 @@ Submodules
- Parameters
-path – the path to the pth file to load the state dict from
+path – the path to the pth file to load the state dict from.
+May also be a SparseZoo stub path preceded by ‘zoo:’ with the optional
+?recipe_type= argument. If given a recipe type, the base model weights
+for that recipe will be loaded.
model – the model to load the state dict into
strict – True to enforce that all tensors match between the model
and the file; False otherwise
diff --git a/sparseml/api/sparseml.tensorflow_v1.datasets.classification.html b/sparseml/api/sparseml.tensorflow_v1.datasets.classification.html
index 20e0de746e8..06945db28ff 100644
--- a/sparseml/api/sparseml.tensorflow_v1.datasets.classification.html
+++ b/sparseml/api/sparseml.tensorflow_v1.datasets.classification.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.datasets.html b/sparseml/api/sparseml.tensorflow_v1.datasets.html
index 497135cbe03..ee8a561e676 100644
--- a/sparseml/api/sparseml.tensorflow_v1.datasets.html
+++ b/sparseml/api/sparseml.tensorflow_v1.datasets.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.html b/sparseml/api/sparseml.tensorflow_v1.html
index f9b9a2e6291..aa5e9633ac0 100644
--- a/sparseml/api/sparseml.tensorflow_v1.html
+++ b/sparseml/api/sparseml.tensorflow_v1.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.models.classification.html b/sparseml/api/sparseml.tensorflow_v1.models.classification.html
index 1ad92199514..5b74b673ff9 100644
--- a/sparseml/api/sparseml.tensorflow_v1.models.classification.html
+++ b/sparseml/api/sparseml.tensorflow_v1.models.classification.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.models.html b/sparseml/api/sparseml.tensorflow_v1.models.html
index 34c9b85ce89..b90a20397ca 100644
--- a/sparseml/api/sparseml.tensorflow_v1.models.html
+++ b/sparseml/api/sparseml.tensorflow_v1.models.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.nn.html b/sparseml/api/sparseml.tensorflow_v1.nn.html
index 0344a8025fa..e8636a9c71e 100644
--- a/sparseml/api/sparseml.tensorflow_v1.nn.html
+++ b/sparseml/api/sparseml.tensorflow_v1.nn.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.tensorflow_v1.optim.html b/sparseml/api/sparseml.tensorflow_v1.optim.html
index eeeebed09dd..ada6b6ef53e 100644
--- a/sparseml/api/sparseml.tensorflow_v1.optim.html
+++ b/sparseml/api/sparseml.tensorflow_v1.optim.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
@@ -319,19 +320,24 @@ Submodules
-
-static
from_yaml
(file_path: str, add_modifiers: Optional[List[sparseml.tensorflow_v1.optim.modifier.Modifier]] = None)[source]¶
-Convenience function used to create the manager of multiple modifiers
-from a yaml file.
+static from_yaml
(file_path: Union[str, sparsezoo.objects.optimization_recipe.OptimizationRecipe], add_modifiers: Optional[List[sparseml.tensorflow_v1.optim.modifier.Modifier]] = None)[source]¶
+Convenience function used to create the manager of multiple modifiers from a
+recipe file.
- Parameters
-file_path – the path to the yaml file to load the modifier from
+file_path – the path to the recipe file to load the modifier from, or
+a SparseZoo model stub to load a recipe for a model stored in SparseZoo.
+SparseZoo stubs should be preceded by ‘zoo:’, and can contain an optional
+‘?recipe_type=<type>’ parameter. Can also be a SparseZoo OptimizationRecipe
+object. i.e. ‘/path/to/local/recipe.yaml’, ‘zoo:model/stub/path’,
+‘zoo:model/stub/path?recipe_type=transfer’
add_modifiers – additional modifiers that should be added to the
-returned manager alongside the ones loaded from the yaml file
+returned manager alongside the ones loaded from the recipe file
- Returns
-ScheduledModifierManager() created from the yaml file
+ScheduledModifierManager() created from the recipe file
@@ -1792,7 +1798,7 @@ Submodules
Sample yaml:
-!ConstantKSModifier
+!ConstantPruningModifier
params: __ALL__
start_epoch: 0.0
@@ -1934,7 +1940,7 @@ Submodules
Sample yaml:
-!GradualKSModifier
+!GMPruningModifier
params: __ALL__
init_sparsity: 0.05
diff --git a/sparseml/api/sparseml.tensorflow_v1.utils.html b/sparseml/api/sparseml.tensorflow_v1.utils.html
index d1d8c1fdb8a..004591f83ab 100644
--- a/sparseml/api/sparseml.tensorflow_v1.utils.html
+++ b/sparseml/api/sparseml.tensorflow_v1.utils.html
@@ -100,7 +100,7 @@
@@ -124,10 +124,11 @@
-
+
diff --git a/sparseml/api/sparseml.utils.datasets.html b/sparseml/api/sparseml.utils.datasets.html
index ed6cb8b417b..7fd2a97ae68 100644
--- a/sparseml/api/sparseml.utils.datasets.html
+++ b/sparseml/api/sparseml.utils.datasets.html
@@ -99,7 +99,7 @@
@@ -129,10 +129,11 @@
-
+
diff --git a/sparseml/api/sparseml.utils.html b/sparseml/api/sparseml.utils.html
index 8fd18393221..4f4267e2a2e 100644
--- a/sparseml/api/sparseml.utils.html
+++ b/sparseml/api/sparseml.utils.html
@@ -100,7 +100,7 @@
@@ -130,10 +130,11 @@
-
+
@@ -479,13 +480,20 @@ Submodules
-
-
sparseml.utils.helpers.
load_recipe_yaml_str
(file_path: str) → str[source]¶
+sparseml.utils.helpers.
load_recipe_yaml_str
(file_path: Union[str, sparsezoo.objects.optimization_recipe.OptimizationRecipe]) → str[source]¶
Loads a YAML recipe file to a string or
-extracts recipe from YAML front matter in a sparsezoo markdown recipe card.
+extracts recipe from YAML front matter in a sparsezoo markdown recipe card.
+Recipes can also be provided as SparseZoo model stubs or OptimizationRecipe
+objects.
YAML front matter: https://jekyllrb.com/docs/front-matter/
- Parameters
-file_path – file path to recipe YAML file or markdown recipe card
+file_path – file path to recipe YAML file or markdown recipe card or
+stub to a SparseZoo model whose recipe will be downloaded and loaded.
+SparseZoo stubs should be preceded by ‘zoo:’, and can contain an optional
+‘?recipe_type=<type>’ parameter. Can also be a SparseZoo OptimizationRecipe
+object. i.e. ‘/path/to/local/recipe.yaml’, ‘zoo:model/stub/path’,
+‘zoo:model/stub/path?recipe_type=transfer’
- Returns
the recipe YAML configuration loaded as a string
diff --git a/sparseml/genindex.html b/sparseml/genindex.html
index 47b2bd138b0..31f2d8b459e 100644
--- a/sparseml/genindex.html
+++ b/sparseml/genindex.html
@@ -98,16 +98,17 @@
-
+
@@ -386,8 +387,6 @@ B
- (sparseml.pytorch.utils.ssd_helpers.MeanAveragePrecision method)
-
-
+
- BatchBenchmarkResults (class in sparseml.pytorch.utils.benchmarker)
- BatchNormParams (class in sparseml.onnx.utils.helpers)
@@ -430,8 +431,12 @@
B
- bucket_iterable() (in module sparseml.utils.helpers)
- - build() (sparseml.tensorflow_v1.datasets.dataset.Dataset method)
+
- build() (sparseml.keras.optim.mask_pruning.MaskedLayer method)
+
+
- build_input_fn() (sparseml.tensorflow_v1.datasets.dataset.Dataset method)
- build_targets() (in module sparseml.pytorch.utils.yolo_helpers)
@@ -784,11 +789,13 @@
D
- dense_block() (in module sparseml.tensorflow_v1.nn.layers)
-
-
+
- freeze_bn_stats_epoch (sparseml.pytorch.optim.modifier_quantization.QuantizationModifier attribute)
+
+ - from_config() (sparseml.keras.optim.mask_pruning.MaskedLayer class method)
- from_dict() (sparseml.onnx.optim.analyzer_model.ModelAnalyzer static method)
@@ -1138,6 +1147,12 @@
G
- get_batch_norm_params() (in module sparseml.onnx.utils.helpers)
+ - get_config() (sparseml.keras.optim.mask_pruning.MaskedLayer method)
+
+
- get_conv_layers() (in module sparseml.pytorch.utils.helpers)
- get_default_boxes_300() (in module sparseml.pytorch.utils.ssd_helpers)
@@ -1233,11 +1248,11 @@
G
- get_nodes_by_output_id() (in module sparseml.onnx.utils.helpers)
- get_numpy_dtype() (in module sparseml.onnx.utils.helpers)
-
- - get_op_input_var() (in module sparseml.tensorflow_v1.utils.variable)
+ - get_op_input_var() (in module sparseml.tensorflow_v1.utils.variable)
+
- get_op_var_index() (in module sparseml.tensorflow_v1.utils.variable)
- get_ops_and_inputs_by_name_or_regex() (in module sparseml.tensorflow_v1.utils.variable)
@@ -1881,6 +1896,8 @@
M
- mask_updater() (sparseml.keras.optim.mask_pruning.MaskedLayer property)
- masked() (sparseml.tensorflow_v1.optim.mask_pruning.PruningOpVars property)
+
+ - masked_layer() (sparseml.keras.optim.mask_pruning.MaskedLayer property)
- MaskedLayer (class in sparseml.keras.optim.mask_pruning)
diff --git a/sparseml/index.html b/sparseml/index.html
index d1871744e2a..239d653e555 100644
--- a/sparseml/index.html
+++ b/sparseml/index.html
@@ -99,16 +99,17 @@
-
+
@@ -178,8 +179,7 @@
SparseML 0.1¶
-Libraries for state-of-the-art deep neural network optimization algorithms,
-enabling simple pipelines integration with a few lines of code
+Libraries for applying sparsification recipes to neural networks with a few lines of code, enabling faster and smaller models
@@ -204,53 +204,52 @@ SparseML 0.1
Overview¶
-SparseML is a toolkit that includes APIs, CLIs, scripts and libraries that apply state-of-the-art optimization
-algorithms such as pruning and
-quantization to any neural network.
-General, recipe-driven approaches built around these optimizations enable the simplification of creating faster
-and smaller models for the ML performance community at large.
-SparseML is integrated for easy model optimizations within the PyTorch,
-Keras, and TensorFlow V1 ecosystems currently.
+SparseML is a toolkit that includes APIs, CLIs, scripts and libraries that apply state-of-the-art sparsification algorithms such as pruning and quantization to any neural network.
+General, recipe-driven approaches built around these algorithms enable the simplification of creating faster and smaller models for the ML performance community at large.
+This repository contains integrations within the PyTorch, Keras, and TensorFlow V1, allowing for seamless model sparsification.
-
-Then if you would like to explore any of the scripts, notebooks, or examples clone the repository and install any additional dependencies as required.
+Then if you would like to explore any of the scripts, notebooks, or integrations clone the repository and install any additional dependencies as required.
@@ -208,7 +209,7 @@ Installation
- Next
+ Next
Previous
diff --git a/sparseml/objects.inv b/sparseml/objects.inv
index ba5d4232af0..1a540491b5e 100644
Binary files a/sparseml/objects.inv and b/sparseml/objects.inv differ
diff --git a/sparseml/py-modindex.html b/sparseml/py-modindex.html
index ba1978defd7..bfdda9791ec 100644
--- a/sparseml/py-modindex.html
+++ b/sparseml/py-modindex.html
@@ -101,16 +101,17 @@
-
+
diff --git a/sparseml/quicktour.html b/sparseml/quicktour.html
index 44323239b59..67ed1a99141 100644
--- a/sparseml/quicktour.html
+++ b/sparseml/quicktour.html
@@ -99,11 +99,11 @@
- Quick Tour
-- PyTorch Optimization
+- PyTorch Sparsification
- Keras Optimization
-- TensorFlow V1 Optimization
-- Estimator-based pipelines
-- Session-based pipelines
+- TensorFlow V1 Sparsification
- Exporting to ONNX
@@ -115,16 +115,17 @@
- Installation
-- Optimization Recipes
+- Sparsification Recipes
-
+
@@ -208,12 +209,11 @@
limitations under the License.
-->
Quick Tour¶
-To enable flexibility, ease of use, and repeatability, optimizing a model is generally done using a recipe file.
-The files encode the instructions needed for modifying the model and/or training process as a list of modifiers.
+
To enable flexibility, ease of use, and repeatability, sparsifying a model is generally done using a recipe.
+The recipes encode the instructions needed for modifying the model and/or training process as a list of modifiers.
Example modifiers can be anything from setting the learning rate for the optimizer to gradual magnitude pruning.
The files are written in YAML and stored in YAML or markdown files using YAML front matter.
-The rest of the SparseML system is coded to parse the recipe files into a native format for the desired framework
-and apply the modifications to the model and training pipeline.
+The rest of the SparseML system is coded to parse the recipes into a native format for the desired framework and apply the modifications to the model and training pipeline.
A sample recipe for pruning a model generally looks like the following:
version: 0.1.0
modifiers:
@@ -242,11 +242,14 @@ Quick Tourhere. Additionally, all code implementations of the modifiers under the optim
packages for the frameworks are documented with example YAML formats.
Pre-configured recipes and the resulting models can be explored and downloaded from the SparseZoo. Also, Sparsify enables autoML style creation of optimization recipes for use with SparseML.
For a more in-depth read, check out SparseML documentation.
-
-PyTorch Optimization¶
-The PyTorch optimization libraries are located under the sparseml.pytorch.optim
package.
-Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into PyTorch training pipelines.
-The integration is done using the ScheduledOptimizer
class. It is intended to wrap your current optimizer and its step function. The step function then calls into the ScheduledModifierManager
class which can be created from a recipe file. With this setup, the training process can then be modified as desired to optimize the model.
+
+PyTorch Sparsification¶
+The PyTorch sparsification libraries are located under the sparseml.pytorch.optim
package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into PyTorch training pipelines.
+The integration is done using the ScheduledOptimizer
class.
+It is intended to wrap your current optimizer and its step function.
+The step function then calls into the ScheduledModifierManager
class which can be created from a recipe file.
+With this setup, the training process can then be modified as desired to sparsify the model.
To enable all of this, the integration code you’ll need to write is only a handful of lines:
from sparseml.pytorch.optim import ScheduledModifierManager, ScheduledOptimizer
@@ -263,10 +266,10 @@ PyTorch Optimization
Keras Optimization¶
-The Keras optimization libraries are located under the sparseml.keras.optim
package.
-Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into Keras training pipelines.
+The Keras sparsification libraries are located under the sparseml.keras.optim
package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into Keras training pipelines.
The integration is done using the ScheduledModifierManager
class which can be created from a recipe file.
-This class handles modifying the Keras objects for the desired optimizations using the modify
method.
+This class handles modifying the Keras objects for the desired algorithms using the modify
method.
The edited model, optimizer, and any callbacks necessary to modify the training process are returned.
The model and optimizer can be used normally and the callbacks must be passed into the fit
or fit_generator
function.
If using train_on_batch
, the callbacks must be invoked after each call.
@@ -294,14 +297,15 @@
Keras Optimization
-TensorFlow V1 Optimization¶
-The TensorFlow optimization libraries for TensorFlow version 1.X are located under the sparseml.tensorflow_v1.optim
package. Inside are APIs designed to make model optimization as easy as possible by integrating seamlessly into TensorFlow V1 training pipelines.
+
+TensorFlow V1 Sparsification¶
+The TensorFlow sparsification libraries for TensorFlow version 1.X are located under the sparseml.tensorflow_v1.optim
package.
+Inside are APIs designed to make model sparsification as easy as possible by integrating seamlessly into TensorFlow V1 training pipelines.
The integration is done using the ScheduledModifierManager
class which can be created from a recipe file.
-This class handles modifying the TensorFlow graph for the desired optimizations.
-With this setup, the training process can then be modified as desired to optimize the model.
+This class handles modifying the TensorFlow graph for the desired algorithms.
+With this setup, the training process can then be modified as desired to sparsify the model.
-Estimator-based pipelines¶
+Estimator-Based pipelines¶
Estimator-based pipelines are simpler to integrate with as compared to session-based pipelines.
The ScheduledModifierManager
can override the necessary callbacks in the estimator to modify the graph using the modify_estimator
function.
from sparseml.tensorflow_v1.optim import ScheduledModifierManager
@@ -317,11 +321,11 @@ Estimator-based pipelines
-Session-based pipelines¶
+Session-Based pipelines¶
Session-based pipelines need a little bit more as compared to estimator-based pipelines; however,
it is still designed to require only a few lines of code for integration.
After graph creation, the manager’s create_ops
method must be called.
-This will modify the graph as needed for the optimizations and return modifying ops and extras.
+This will modify the graph as needed for the algorithms and return modifying ops and extras.
After creating the session and training normally, call into session.run
with the modifying ops after each step.
Modifying extras contain objects such as tensorboard summaries of the modifiers to be used if desired.
Finally, once completed, complete_graph
must be called to remove the modifying ops for saving and export.
diff --git a/sparseml/recipes.html b/sparseml/recipes.html
index 1a7855ca178..55475bdbd91 100644
--- a/sparseml/recipes.html
+++ b/sparseml/recipes.html
@@ -7,7 +7,7 @@
- Optimization Recipes — SparseML 0.1.0 documentation
+ Sparsification Recipes — SparseML 0.1.0 documentation
@@ -100,7 +100,7 @@
- Quick Tour
- Installation
-- Optimization Recipes
+- Sparsification Recipes
- Modifiers Intro
- Training Epoch Modifiers
- Pruning Modifiers