DeviceContext should not be `const T&` in `Operator::Run(...)`
Created by: reyoung
Some method of DeviceContext
is not const, such as
class GPUDeviceContext: public DeviceContext {
cuda_stream_t Stream();
cunn_handle_t CudnnHandle();
};
The code is here. It because the handlers and stream are initialized lazily, i.e., they will not be initialized only until they are used. So DeviceContext
is a mutable object, not const object.
But in Operator::Run
, we use DeviceContext
like Operator::Run(..., const DeviceContext& context);
. So if we want to use cudaStream
or cudnnHandle
, we must const_cast(context)
to drop const
qualifier. The code like
auto cudnn_handle = const_cast<DeviceContext&>(context).cudnnHandle();
However, const_cast
is very harmful and should be forbidden in our code.