# Crate caffe2_transform

source ·## Macros

- | The Transform Base Object | | A Transform is an operation which manipulates | a Caffe2 NetDef. | | You can consider it as a function: | | Transform.ApplyTo(NetDef) -> NetDef | | A Transform Operation does 4 things: | | 1) Creates a Graph object from a NetDef, | which stores connections. | | 2) Pattern Matches on the Graph, to | find subgraphs it wants to change. | | 3) Replaces the subgraphs that it’s | matched with new operators. | | 4) Creates a NetDef from the changed | Graph, and returns it. | | The effect of a Transform is defined | by its 3 protected virtual functions. | | 1) PatternRule determines for an ordered | subgraph and a node, whether to consider | adding the node to the subgraph. | | 2) ValidatorRule determines, for | an ordered subgraph, whether it is a | match. | | 3) ReplaceRule mutates the graph, | based on a matched subgraph. | | This is the base class for all derived | classes to base off. To create your own | transform, write your implementations | for PatternRule, ValidatorRule, and | | ReplaceRule. |

## Structs

- | Common Subexpression Elimination | | This transforms looks for specific operators | (denoted by allowed_ops_), and removes | unnecessary repetition of that operator. | | Consider some operator of X, that reads from | blob b_ written to by W. X_a and X_b read the | output of X. However, another operator Y, is | the same type as X, has the same arguments as | X, and reads from the same input b_, written to | by W. It’s output is the same as X. Y_a, Y_b, | and Y_c read from Y. | | Then, we can eliminate the common | subexpressions X and Y, and merge them to Z, | where X_a, X_b, Y_a, Y_b, and Y_c all read from | Z. | | | TODO(benz): Fix the error to not match nodes | that write to external output.
- | This TransformDummy transform will | find all subgraphs of shape | | (TransformDummyOp1 -> TransformDummyOp2) | and replaces them with (TransformDummyOp3). | Simple unit test. |

- | PatternNetTransform allows you to | create transforms using a simple interface. | | Simply provide a Pattern NetDef and | a Replace | | NetDef, and this Transform will find | subgraphs which fit the pattern net, | and replace it with the replace net. |

- This TransformDummy transform will find all operators of type old_type, and replace them with type new_type.

## Enums

- | Determines the type of subgraphs that | | PatternMatch will find. | | CONNECTED_SUBGRAPH will only match | subgraphs that are connected. | | These subgraphs satisfy that every | node of the match is connected to the | subgraph of the nodes that come before | it. | | For example, in the graph (1) –> (2) | –> (3) –> (4), | | This is capable of matching the subgraph | [2, 3] and [4, 3] | | This is not capable of matching the subgraph | [2, 4]. | | SORTED_WRT_EXECUTION_ORDER will | match subgraphs that guarantee sorted | execution order. | | The nodes don’t have to be connected. | It is faster than General. | | For example, in the graph (1) –> (2) | –> (3) –> (4), | | This is capable of matching the subgraph | [2, 4], [3, 4]. | | This is not capable of matching the subgraph | [3, 1], [4, 3]. | | GENERAL can match any subgraph. | | For example, in the graph (1) –> (2) | –> (3) –> (4), | | This is capable of matching subgraphs | [2, 4], [3, 4], [4, 2, 1]. | | There is no ordered subgraph of G that | cannot be matched by this. |

## Traits

- | Single Op Transform Base class | | A transform which is applied to a single | node, in place. | | Transforms which derive from SingleOpTransform | need to override: | | ReplaceOperator and MatchOperator. |

## Functions

- | Create a Transform object from registry, | and immediately apply it to a Netdef. |
- | Create a Transform object from registry, | apply it to a NetDef. | | Will only return the transformed net | if it is faster than the old net. | | This will run the init net first, will | run the two nets warmup_runs times. | | Then, we will take the average time of | main_runs runs, and only keep the transformed | net if it is faster by a factor of improvement_threshold. |
- | Checks if the node at model_idx and the node | at candidate_idx are “common | subexpressions”. That is, do they have the | same function, and take in the exact same | input. If so, then their function is | duplicated.
- | Creates a Transform based on a key, which | should be defined in registry. |