Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

Problem about `use_mkldnn` flag

Open luotao1 opened this issue 8 years ago • 15 comments

This issue is from Intel sides.

The problem we have with use_mkldnn flag is that we need to modify all models to enable newly integrated MKL-DNN ops. It would be great, if we can only add an integration and in case when Fluid is run with use mkldnn parameter, all available Ops are used without modification of model scripts, to pass the option.

luotao1 avatar May 18 '18 04:05 luotao1

I think that makes sense.

Superjomn avatar May 21 '18 07:05 Superjomn

How about using MKLDNNPlace() to call all available OPs?

place=fluid.MKLDNNPlace()

luotao1 avatar May 21 '18 09:05 luotao1

It would be great to have global use_mkldnn flag. Now it is very difficult to fallback this flag to some ops, e.g.: elementwise_add, because the python code is auto generate with generate_layer_fn(_OP). Also it would be nice to have another flag not_use_mkldnn on layer level, to switch off MKL-DNN from some layers/ops.

kbinias avatar May 21 '18 10:05 kbinias

@luotao1 Thank you for this issue.

I will use the sum operator as an example to describe this problem. So, during the implementation of this operator I have encountered a few difficulties. The current version of the sum operator of MKLDNN algorithm is using inside of the other operators. Therefore the flag can not be uses in this way. For instance: Backward.py

To that end I agree with @kbinias.

mozga-intel avatar May 21 '18 11:05 mozga-intel

it would be nice to have another flag not_use_mkldnn on layer level, to switch off MKL-DNN from some layers/ops.

That makes sense, the user can

  • globally set a default config, ON or OFF
  • set MKLDNN config separately for specific operator

For example, such use case (the name of APIs is not important)

set_default_use_mkldnn(True)

v1 = op0(v0) # use mkldnn by default if supported
# ...
v2 = op1(v1, use_mkldnn=False) # turn off MKLDNN for this operator
# ...

The above function is better.

Superjomn avatar May 22 '18 03:05 Superjomn

We will have a discussion about how to add this global flag function in our framework ASAP.

luotao1 avatar May 22 '18 03:05 luotao1

@luotao1 Any ETA for this?

mrysztow avatar Jun 06 '18 08:06 mrysztow

@mrysztow Discussed with @Superjomn and @tensor-tang, the newest design is:

  • use an environment variable export FLAGS_use_mkldnn=True to control it.
  • add EnableMKLDNN function (maybe rename it) in paddle/fluid/platform/mkldnn_helper.h https://github.com/PaddlePaddle/Paddle/blob/406c1dd143a66b29e6f40675d0a207648b260e6c/paddle/fluid/inference/tests/test_helper.h#L136-L146
  • call EanbleMKLDNN function in Executer:Run (paddle/fluid/framework/executer.cc) to transpiler the program.

We will finish it ASAP before next meeting.

luotao1 avatar Jun 06 '18 09:06 luotao1

@luotao1 What's the reason for using an environment variable? Does it mean that some parameters will be passed as script's arguments and some of them will be passed as environment variables?

tpatejko avatar Jun 08 '18 09:06 tpatejko

Does it mean that some parameters will be passed as script's arguments and some of them will be passed as environment variables?

I think an environment variable might be more general for both python scripts/c++ bin.

Or do you have any suggestion? @tpatejko

Superjomn avatar Jun 08 '18 09:06 Superjomn

@Superjomn I think it is a good idea, as now we need to change all model scripts to add this option. But will it be consistent with focing usage of other backends, like gpu?

mrysztow avatar Jun 08 '18 13:06 mrysztow

That depends, the MKLDNN might be the first one that has a global switch in runtime.

I suggest we use the environment variable first, with two singleton functions in both python and C++ to wrap it, called static bool DoUseGlobalMKLDNN() and def do_use_global_mkldnn()(name need to futher considered), and use this way first.

If the environment variable's name or this plan is changed, just update those two function is easier and flexible. @mrysztow

Superjomn avatar Jun 09 '18 02:06 Superjomn

@mrysztow You can use FLAGS_use_mkldnn=true to control it now, see #11319 for details.

luotao1 avatar Jun 11 '18 04:06 luotao1

@Superjomn My main concern was some sort of consistency and intuition behind passing parameters to the models scripts. I mean, a user will have to set an environment variable if they want to run something on CPU with MKLDNN, which is different from running something on GPU, that requires passing an argument to a model script.

However, it's true what you're saying, this approach is more flexible, as it doesn't require changing scripts with models.

tpatejko avatar Jun 11 '18 09:06 tpatejko

Yeah, I understand your concern.

the MKLDNN might be the first one that has a global switch in runtime.

The environment variable is a low-level API and easy to hide in many ways.

We can concentrate on the implementation of MKLDNN functions first, and have another discussion about this API after MKLDNN functions are ready and release to users.

Superjomn avatar Jun 13 '18 01:06 Superjomn