Caffe2中FillOp与GaussianFill的run_once含义及定义位置问询
Hey there! Let's break down your questions about Caffe2's FillOp and GaussianFill clearly:
1. What does run_once mean in Caffe2's FillOp?
FillOp is the base operator for populating tensors with specific values or statistical distributions. The run_once flag is a boolean parameter that controls whether the filling operation runs only once rather than every time the operator is invoked.
Here's the practical gist: If you set run_once=True, the tensor gets filled the first time the operator runs (usually during the first forward pass). On all subsequent runs, the operator skips the filling step entirely. This is perfect for tensors that only need one-time initialization—like fixed constants, pre-trained weights, or static lookup tables—since it cuts down on redundant computation and saves resources.
If run_once=False (the default in most cases), the filling operation will execute every time the operator is called. This is rare for fill ops, but might be useful if you need to refresh the tensor's values on every pass (though that's more of an edge case).
2. Where is run_once defined for GaussianFill?
GaussianFill is a specialized version of FillOp that fills tensors with values sampled from a Gaussian (normal) distribution. Since it inherits directly from the FillOp base class, the run_once parameter isn't redefined in GaussianFill's specific code—it's inherited from the base class.
To pinpoint its origins in the Caffe2 source code:
- The initial declaration of the
run_onceparameter lives in the FillOp base class, typically in the fileoperators/fill_op.h. Here, it's defined as a boolean argument in the operator's parameter schema. - The logic that enforces the
run_oncebehavior (checking if the tensor is already initialized before attempting to fill it) is implemented in the FillOp's forward pass code, found inoperators/fill_op.cc. - For
GaussianFillspecifically, when it's registered as an operator (inoperators/gaussian_fill_op.cc), it reuses the base FillOp's parameter schema. That meansrun_onceis included automatically—GaussianFill only overrides the logic for generating Gaussian-distributed values, while leaving therun_oncecheck to the base class.
内容的提问来源于stack exchange,提问作者David Zhao

