Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need to refine the design of DeviceContext #6415

Closed
QiJune opened this issue Dec 8, 2017 · 0 comments · Fixed by #6433
Closed

need to refine the design of DeviceContext #6415

QiJune opened this issue Dec 8, 2017 · 0 comments · Fixed by #6433

Comments

@QiJune
Copy link
Member

QiJune commented Dec 8, 2017

There are two problems of current DeviceContext design:

  1. The base class DeviceContext has a interface called GetEigenDevice.
    class DeviceContext {
    public:
    virtual ~DeviceContext() {}
    virtual Place GetPlace() const = 0;
    template <typename PlaceType,
    typename DeviceType =
    typename EigenDeviceConverter<PlaceType>::EigenDeviceType>
    DeviceType* GetEigenDevice() const;
    virtual void Wait() const {}
    virtual void Finish() const {}
    };

However, Eigen is not supported in all kinds of Device, e.g. AMD Graphics card. It should be moved to derived DeviceContext class.

  1. The math functors in operators/math directory take Place template parameter.
    template <>
    void gemm<platform::GPUPlace, float>(const platform::DeviceContext& context,
    const CBLAS_TRANSPOSE transA,
    const CBLAS_TRANSPOSE transB, const int M,
    const int N, const int K,
    const float alpha, const float* A,
    const float* B, const float beta,
    float* C) {
    // Note that cublas follows fortran order, so the order is different from
    // the cblas convention.
    int lda = (transA == CblasNoTrans) ? K : M;
    int ldb = (transB == CblasNoTrans) ? N : K;
    cublasOperation_t cuTransA =
    (transA == CblasNoTrans) ? CUBLAS_OP_N : CUBLAS_OP_T;
    cublasOperation_t cuTransB =
    (transB == CblasNoTrans) ? CUBLAS_OP_N : CUBLAS_OP_T;
    PADDLE_ENFORCE(platform::dynload::cublasSgemm(
    reinterpret_cast<const platform::CUDADeviceContext&>(context)
    .cublas_handle(),
    cuTransB, cuTransA, N, M, K, &alpha, B, ldb, A, lda, &beta, C, N));
    }

We have to cast the DeviceContext to CUDADeviceContext even though we have already know we are implementing a CUDA version of the functor.
Instead, we'd better to take DeviceContext as the template parameter.

At the same time, the template parameter in OpKernel should also be DeviceContext instead of Place

This was referenced Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant