Skip to content

Add cupy.RawModule()#2389

Merged
emcastillo merged 9 commits intocupy:masterfrom
leofang:rawmodule
Aug 19, 2019
Merged

Add cupy.RawModule()#2389
emcastillo merged 9 commits intocupy:masterfrom
leofang:rawmodule

Conversation

@leofang
Copy link
Copy Markdown
Member

@leofang leofang commented Aug 9, 2019

Summary

Rationale

(This also serves as a refreshment of what's discussed/done early on...)

  • As initially discussed in documenting cupy.cuda.function.Module? #1657, it'd be nice if an API can be exposed so that legacy CUDA codebase can be "imported" in CuPy;
  • It seems that cupy.cuda.function.Module() was intended to be used as private API;
  • It was suggested that RawKernel.compile(long_code) can be used as a public API, which was implemented in Add RawKernel.compile() method #1889;
  • However, RawKernel.compile() returns a Module instance, not RawKernel, which could be misleading/confusing;
  • With the recent work Allow getting and setting CUDA kernel attributes #2369, RawKernel now has function attributes, and it'd be nice if each of the CUDA kernels compiled from the same source code can possess attributes independently, but Add RawKernel.compile() method #1889 does not meet this need;
  • To achieve this, the new implementation cupy.RawModule.get_function(kernel_name) returns a RawKernel instance;
  • cupy.RawModule() can be initialized either by a CUDA source code (same as RawKernel; the compilation is done at initialization) or by loading an existing CUDA cubin.

I'd like to discuss with core devs to see if this PR makes better sense before proceeding to docs, tests (tested locally), and further refinement. Thanks.

UPDATE: The module name is changed to RawModule as discussed below.

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 13, 2019

Rebased to master since #2369 is merged.

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 14, 2019

Hi @emcastillo, any comment or suggestion? I am fine with either a red light or a green light.

@beam2d
Copy link
Copy Markdown
Contributor

beam2d commented Aug 15, 2019

The basic direction looks good to me. I would like to reconsider the naming of the classes/methods including existing ones because they look confusing. The current PR uses the following names:

  • cupy.core.function.Function
  • cupy.core.function.Module
    • .get_function method, which returns cupy.core.function.Function
  • cupy.core.RawKernel
    • .kernel property, which returns cupy.core.function.Function
  • cupy.core.RawModule (new)
    • .get_function method (new), which returns cupy.core.RawKernel
  • cupy.RawKernel: an alias of cupy.core.RawModule
  • cupy.Module (new): an alias of cupy.core.RawModule

The following is one idea of renaming them:

  • cupy.core.function.Module -> cupy.core.function.RawModule (we may leave the old name for compatibility, but this API is internal so this is not mandatory)
  • cupy.core.RawKernel.kernel property -> .function property (old name is also left for compatibility)
  • cupy.core.RawModule -> cupy.core.Module
  • cupy.core.RawModule.get_function method -> .get_kernel method

If it sounds weird, feel free to have a discussion here.

@emcastillo Could you also check the implementation?

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 15, 2019

Hi @beam2d, thank you for kicking off the discussion! Let me first correct a few typos in your nice summary for the current PR:

  • cupy.cuda.function.Function
  • cupy.cuda.function.Module
    • .get_function method, which returns cupy.cuda.function.Function
  • Alias: cupy.cuda.Function -> cupy.cuda.function.Function
  • Alias: cupy.cuda.Module -> cupy.cuda.function.Module
  • cupy.core.raw.RawKernel
    • .kernel property, which returns cupy.cuda.function.Function
  • cupy.core.raw.RawModule (new)
    • .get_function method (new), which returns cupy.core.raw.RawKernel
  • Alias: cupy.RawKernel -> cupy.core.RawKernel -> cupy.core.raw.RawKernel
  • Alias (new): cupy.Module -> cupy.core.RawModule -> cupy.core.raw.RawModule

(The corrections are 1. core->cuda in a few places, and 2. cupy.RawKernel is an alias of cupy.core.RawKernel, not RawModule. I also added all alias for clarity.)

I am fine with the renaming as long as you think it's ok to make changes to internal APIs. My preference is to not rename; instead, perhaps simply exposing RawModule all the way (cupy.RawModule -> cupy.core.RawModule -> cupy.core.raw.RawModule) is easier for users familiar with RawKernel to figure out that RawModule will hold a collection of RawKernels. But I can be convinced.

In addition, I also considered

cupy.core.RawModule.get_function method -> .get_kernel method

when creating the PR. I didn't choose the name get_kernel as I wanted to make cupy.Module.get_function smell like a wrapper (and it indeed is!) of cuModuleGetFunction, but I don't mind making the change.

Thanks!

@beam2d
Copy link
Copy Markdown
Contributor

beam2d commented Aug 15, 2019

Oh thank you for fixing many typos, I was so careless! Just exposing the name RawKernel in cupy. level sounds good to me, addressing my concern on confusion between cupy.cuda.function.Module and cupy.Module. Let's go with it.

As for get_function vs get_kernel, I now think using get_function is rather good. I was originally thinking about the consistency of the terminology in cupy namespace. I now think it is good for users (esp. those who are not very used to CuPy) to be aware of the existence of two terms (kernel and function) referring to almost the same concept. Using a name analogical to cuModuleGetFunction sounds convincing in this sense.

@emcastillo
Copy link
Copy Markdown
Member

I'll take a look at it tomorrow first thing on the morning! :)
Thanks!

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 15, 2019

Just exposing the name RawKernel in cupy. level sounds good to me,

@beam2d did you mean RawModule? 😆

addressing my concern on confusion between cupy.cuda.function.Module and cupy.Module. Let's go with it.

As for get_function vs get_kernel, I now think using get_function is rather good. I was originally thinking about the consistency of the terminology in cupy namespace. I now think it is good for users (esp. those who are not very used to CuPy) to be aware of the existence of two terms (kernel and function) referring to almost the same concept. Using a name analogical to cuModuleGetFunction sounds convincing in this sense.

OK glad to know we are aligned! For others please feel free to leave your feedback. I'll start making changes today, but this is a dynamic PR and things can change if deemed necessarily for providing a sustainable and useful public API.

@leofang leofang changed the title [WIP] Add cupy.Module() [WIP] Add cupy.RawModule() Aug 15, 2019
@emcastillo
Copy link
Copy Markdown
Member

The current implementation LGTM, it is clean and straightforward.

@beam2d
Copy link
Copy Markdown
Contributor

beam2d commented Aug 16, 2019

@beam2d did you mean RawModule? 😆

Ah, yes! I should take more sleep...zzz

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 16, 2019

Ah, yes! I should take more sleep...zzz

🤝

@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 16, 2019

OK, based on the above discussion I made a few small changes. I also added docstring, tutorial, and test. This is ready for review now, @emcastillo. If you'd like to wait and see if other CuPy devs have feedback and/or suggestions, by all means. Thanks!

This is off topic: Since I touched kernel.rst, I was tempted to take over #1866 and include that part of doc...I didn't do that though.

@leofang leofang marked this pull request as ready for review August 16, 2019 03:09
@leofang leofang changed the title [WIP] Add cupy.RawModule() Add cupy.RawModule() Aug 16, 2019
@emcastillo
Copy link
Copy Markdown
Member

The PR looks good to me, but since its a big feature I would like other people to check it too.

@emcastillo emcastillo requested a review from asi1024 August 16, 2019 09:14
Copy link
Copy Markdown
Member

@emcastillo emcastillo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@emcastillo
Copy link
Copy Markdown
Member

Jenkins, test this please

@chainer-ci
Copy link
Copy Markdown
Member

Jenkins CI test (for commit aa5199e, target branch master) succeeded!

@emcastillo emcastillo added the cat:feature New features/APIs label Aug 18, 2019
@emcastillo emcastillo added this to the v7.0.0b3 milestone Aug 18, 2019
@emcastillo emcastillo merged commit 07d878a into cupy:master Aug 19, 2019
@leofang
Copy link
Copy Markdown
Member Author

leofang commented Aug 19, 2019

Thanks to all CuPy core devs! It's been a long way since my first PR #1889!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

documenting cupy.cuda.function.Module?

4 participants