Default prop_kind for a convolution in TensorFlow/PyTorch
Good morning.
When TensorFlow creates a new forward convolution (mkl_conv_ops.cc) default prop_kind is forward_training even if a code runs in inference regime. On the other hand oneDNN documentation advises to use forward_inference:
Always use dnnl::prop_kind::forward_inference when running inference
May you please explain how to satisfy these recommendations on propagation kind and workspace usage when running oneDNN as backend library within a framework (TensorFlow, PyTorch)? Are there any drawbacks in deploying inference with the primitives set up as prop_kind::forward_training?
Best regards, Aleksandr
Hi @alenik01 ,
Inference vs Training
The difference between inference and training is explained in documentation.
Inference vs Training: Workspace
The most important part is that forward_training is expected to be coupled with backward pass, in this case a primitive implementation could prepare workspace memory by precomputing some stuff to speed up backward computations. So potentially forward_training might be slower because it prepares some additional data for backward pass. In case of convolution we don't have implementations which prepare workspace in case of training, but if you look into other primitives, for example, LRN or Pooling, there are implementations which prepare workspace in case prop_kind is forward_training.
Inference vs Training: Workspace: hint_fwd_pd
To make sure that forward implementation is compatible with backward implementation forward primitive descriptor is passed as hint_fwd_pd to backward primitive descriptor creation. During backward primitive descriptor creation each implementation inspects hint_fwd_pd and if forward implementation is not compatible init() returns unimplemented and next implementation does the same until end of implementation list is reached.
Integration: TensorFlow
As I understand in TensorFlow forward and backward passes are isolated, so there is no way to pass hint_fwd_pd from forward to backward. To workaround this issue oneDNN integration of backward pass creates "dummy" forward primitive descriptor, which is then passed to backward primitive descriptor creation. This is unsafe, because if anything changed between forward and backward passes (for example, number of threads is different) we might get different forward implementation in "dummy" forward primitive descriptor which will be used for backward implementation and workspace passed (or not passed at all) at execution might not match the one expected by backward pass.
The reasons TF works fine with forward_training in case of convolution for both inference and training:
- oneDNN convolutions don't utilize workspace mechanism (which is actually an implementation detail);
- oneDNN allows to append post operations even if
forward_trainingis passed, which is fine if post ops are used during framework inference, but doesn't make much sense during training because usually intermediate memory buffers are required to be able to compute gradients.
So ideally if frameworks want to utilize oneDNN workspace mechanism to speedup training they should follow oneDNN programming model by creating oneDNN primitives with proper prop kind and making sure hint object is passed to backward or at least an identic one can be obtained on backward.
Summoning @agramesh1 @Jianhui-Li for more details about TF integration limitations
Regards, Igor Safonov
Good afternoon, @igorsafo. Many thanks for the detailed answer.
So ideally if frameworks want to utilize oneDNN workspace mechanism to speedup training they should follow oneDNN programming model by creating oneDNN primitives with proper prop kind
My impression from TF codebase is that it employs the same isolated forward pass both for training and inference without setting the proper prop_kind in the case of inference.
Hi @alenik01, @mdfaijul is looking into using forward_inference for inference.
Sorry to comment on a closed issue @mgouicem @mdfaijul, but I have recently started looking into using forward_inference when doing inference and I'm curious if there's been any progress here and what is the plan? Thanks!
@alenik01 @mgouicem I checked ResNet50 model with forward_inference, but did not notice performance difference in TensorFlow.
@mdfaijul oneDNN might still return different implementations for forward_inference and forward_training. It might not matter for ResNet50 or for x64, but it could have an impact for other topologies.
For example, when using any for data tensors on x64, we typically default to plain layouts when using forward_inference, but we use blocked layouts for forward_training. Other backends (e.g. ARM) might have other requirements to dispatch their best implementations. @diaena @alenik01 could you clarify what are the implications of forward_inference/forward_training on aarch64 backend? From quick code inspection, I could not find any difference in dispatching.
Hi, I'm using ResNet50 with PyTorch+OneDNN in eval (inference mode) and observed the same, "forward_training" is invoked for convolution primitives. Can someone here comment if you managed to change it to "forward_inference"? if yes, did you observe any performance gain?
+@jgong5 for PyTorch.
Note that forward_inference can dispatch different implementations than forward_training. Those might use different data layouts (as mentioned in comments above), and different performance (e.g. for Winograd convolutions, we do weights transform during reorder for inference, but inside convolution computation for forward_training).
From the framework side, there is no notion of "training" or "inference" for most of the ops at the graph level or op kernel level. Frameworks can only know if a tensor is constant or not in the graph mode. In the case of Winograd conv that @mgouicem pointed out, does a constant weight imply "forward_inference" here?
@jgong5 If constant weights imply that there will be no backward pass, then yes you can use "forward_inference" when weights are constant.
If constant weights imply that there will be no backward pass, then yes you can use "forward_inference" when weights are constant.
No, it only implies no backward for weight since it is constant. In the case of Winograd conv, why do you handle the training and inference differently? Is it an implementation choice?
+@jgong5 for PyTorch.
Note that forward_inference can dispatch different implementations than forward_training. Those might use different data layouts (as mentioned in comments above), and different performance (e.g. for Winograd convolutions, we do weights transform during reorder for inference, but inside convolution computation for forward_training).
Hi @jgong5 , I tested benchdnn convolution primitive for 'forward_inference' and 'forward_training' for the same shape, and there is no performance (latency) difference between the two! Do you think at graph level there would be a performance difference between the two?
Here is what I used for comparison.
#For forward_inference:
./benchdnn --conv --engine=cpu --cfg=f32 --mode=P --dir=**FWD_I** --batch=inputs/conv/shapes_resnet_50
#For forward_training:
./benchdnn --conv --engine=cpu --cfg=f32 --mode=P --dir=**FWD_B** --batch=inputs/conv/shapes_resnet_50
From the DNNL VERBOSE logs it looks like both were using the same formats, shapes. and this is with alg:convolution_direct.
Thanks!
No, it only implies no backward for weight since it is constant.
Is there a training use case where user is only interested in the activation gradient but never update weights? So far, we always assumed that training implies non constant weights (since training implies updating weights). Is that assumption wrong? If not, then you can safely use forward_inference when weights are constant.
In the case of Winograd conv, why do you handle the training and inference differently? Is it an implementation choice?
This is because for inference, we assume that the user can reorder weights once (and pre-compute WInograd weights transformation at the same time), and that reordered weights are reused in multiple inference runs. For training, we assume that for each batch, we need to compute forward, data gradient and weights gradient. So to minimize reorders between these 3 steps, we align layouts, and keep the Winograd transformations inside the primitive executions.
I tested benchdnn convolution primitive for 'forward_inference' and 'forward_training' for the same shape, and there is no performance (latency) difference between the two!
The fact that there is no performance difference now, does not mean that it will always be true. This depends on both oneDNN optimizations, and oneDNN integration in PyTorch (or any framework). For example, if oneDNN integration to PyTorch does not use Winograd convolution, the above performance difference between forward_training and forward_inference will not show up when running PyTorch.
Is there a training use case where user is only interested in the activation gradient but never update weights? So far, we always assumed that training implies non constant weights (since training implies updating weights). Is that assumption wrong? If not, then you can safely use forward_inference when weights are constant.
The assumption is wrong. There are cases like transfer learning or sensitivity analysis that require backward without updating weights.
For training, we assume that for each batch, we need to compute forward, data gradient and weights gradient. So to minimize reorders between these 3 steps, we align layouts, and keep the Winograd transformations inside the primitive executions.
If frameworks can cache the reordered weights given they are constant, can you also design the layout algorithms for training the same way as inference?
Thanks for the clarification @jgong5 !
If frameworks can cache the reordered weights given they are constant, can you also design the layout algorithms for training the same way as inference?
Currently we don't have any mechanism in the library that would allow to take advantage of constant weights in backward_data, or that would optimize layouts for forward and backward_data only with constant weights (so without consideration for backward_weights and ignoring weights reorder overheads).
The best a user can do with constant weights for the case you mentioned above (forward + backward_data with constant weights) is:
- use forward_inference when no workspace is passed from forward to backward. This allows to maximize the amount of pre-computation we can do during weights reordering, and allows us to use the most optimized weights layout for forward.
- use the same activation layout between forward_inference and backward_data. This allows to reduce the amount of reorders between forward and backward by data. This should also be made more optimal soon as we transition to plain layout activations for both inference and training.
In the future, we might consider adding a way to specify that weights are constant, so that not only forward can benefit from constant weights, but backward_data can too.