question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simple way to add Basic Ops in ChainerX

See original GitHub issue

While working on the PR to add sin and cos functionality. I found that most of the code was same except for a verysmall part.

Also going through the PR #6590 , it seems that a lot of same code needs to be added/modified at various places.

So I tried to do it with a simple Macro based on Tanh implementation.

// Can be improved !!??
#define UnaryOp_Cuda(func, func_def)                                                \
    namespace {                                                                     \
    template <typename T>                                                           \
    struct func##Impl {                                                             \
        using CudaType = cuda_internal::DataType<T>;                                \
        __device__ func_def                                                         \
    };                                                                              \
    }                                                                               \
                                                                                    \
    void CudaDevice::func(const Array& x, const Array& out) {                       \
        CheckDevicesCompatible(x, out);                                             \
        CudaSetDeviceScope scope{index()};                                          \
        const Array& x_cast = x.dtype() == out.dtype() ? x : x.AsType(out.dtype()); \
        VisitFloatingPointDtype(out.dtype(), [&](auto pt) {                         \
            using T = typename decltype(pt)::type;                                  \
            Elementwise<const T, T>(func##Impl<T>{}, x_cast, out);                  \
        });                                                                         \
    }

which allows to define a new Operator simply by

UnaryOp_Cuda(Sqrt, void operator()(int64_t /*i*/, CudaType x, CudaType& out) { out = cuda::Sqrt(x); })

From a glance it can also be extended for many standard Binary Operators.

I have tried this with macro for native and converted similar Unary Operators and it built successfully. All changes can be found here

Also it seems important to have simple code generation script to facilitate these simple cases.

- name : Sqrt
- operator_type : Unary
- transform : out = Sqrt(in)

- name : Cube
- operator_type : Unary
- transform : out = (in * in * in)

The above yaml example could be easily parsed to generate Header file and Implementation for Standard Operation for Cuda and Native or any other backend( Unary , Binary ). It will also facilitate making bulk changes with ease.

Would love to know your views on this. Thank You.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
kshitij12345commented, Apr 9, 2019

Oh, @hvy @niboshi , thanks for taking interest. Will soon hit you with a PR.

2reactions
hvycommented, Apr 9, 2019

A drop by comment by how about ELTWISE instead of ELMWISE? The former seems to be more common (Google search hits). I observed it in contexts/source code of TF, CUDA and Caffe at least.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ChainerX Tutorial — Chainer 7.8.1 documentation
The following example demonstrates how you can create an array and access its most basic attributes. Note that the APIs are identical to...
Read more >
ChainerX needs to support more routines · Issue #6423
E.g. Device::Arange -> ArangeOp : public Op. The following description ... Declare the routine as a chainerx::Array method if appropriate.
Read more >
Chainer and ChainerX
Variables hold how they were computed. Use it to ... Domain specific add-on packages ... ChainerX. Basic ops. Integration to Chainer. Wide coverage...
Read more >
ChainerX and How to Take Part
Routines (1/2) • Backpropable autograd operations on chainerx::Arrays • chainerx::{ Add,Subtract,Multiply,Divide, Sum,Transpose,Reshape,Dot, ...
Read more >
GSoC 2019 Summary : Building RNNs For ChainerX
I worked on building Recurrent Neural Networks for ChainerX. ... The backend is pluggable so that it is much easier to add a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found