Ex Exten endi ding ng Num umba FOSDEM 2019 Joris Geessels // - - PowerPoint PPT Presentation

ex exten endi ding ng num umba
SMART_READER_LITE
LIVE PREVIEW

Ex Exten endi ding ng Num umba FOSDEM 2019 Joris Geessels // - - PowerPoint PPT Presentation

Ex Exten endi ding ng Num umba FOSDEM 2019 Joris Geessels // @jolos JIT compiler (LLVM) @numba.jit(nopython=True) def go_fast(a): trace = 0 accelerates Python for i in range(a.shape[0]): trace += numpy.tanh(a[i, i]) numpy support


slide-1
SLIDE 1

Ex Exten endi ding ng Num umba

FOSDEM 2019 Joris Geessels // @jolos

slide-2
SLIDE 2

2

@numba.jit(nopython=True) def go_fast(a): trace = 0 for i in range(a.shape[0]): trace += numpy.tanh(a[i, i]) return a + trace x = numpy.arange(100).reshape(10, 10) print(go_fast(x)) accelerates Python JIT – compiler (LLVM) numpy support ++

slide-3
SLIDE 3

3

Si Simu mula latio tion n of a PI PIC

class WaveguideModel(CompactModel): def calculate_smatrix(params, env, S): phase = 2 * np.pi / env.wavelength * params.n_eff * params.length A = 0.99 S['in', 'out'] = S['out', 'in'] = A * np.exp(1j * phase)

dict support? C++ objects? Called by simulator?

slide-4
SLIDE 4

Lowering (codegen) Rewrite (+ types) Type Inference Rewrite IR Bytecode → Numba IR

④ custom codegen

4

Num umba Co Comp mpile iler Pip ipel elin ine

① custom rewrite ② add types + inference ③ add datamodels

slide-5
SLIDE 5

5

① Num umba IR IR R Rew ewrit ite

from numba import ir from numba.rewrites import Rewrite, register_rewrite # 'before-inference' or 'after-inference' @register_rewrite('before-inference') class MyRewrite(Rewrite): def match(self, func_ir, block, typemap, calltypes): # search for expressions to rewrite, # return True when match return True def apply(self): # return a new function 'block’ return new_block

slide-6
SLIDE 6

6

② Type pe In Infer erence ence

# 2 public decorators to register your custom typers from numba.extending import type_callable, typeof_impl class MyPointType(numba.types.Type): # A custom type to represent a point # used during inference def __init__(self): super(MyPointType, self).__init__(name='Point') @type_callable(MyPoint) def type_MyPoint(context): def typer(x, y): # your_func returns a point return MyPointType() return typer

instantiate & return your custom type

slide-7
SLIDE 7

7

③ Lower erin ing g Type pes

Lowering = generating LLVM intermediate representation (IR)

from numba.extending import register_model, models @register_model(MyPointType) class MyPointModel(models.StructModel): def __init__(self, dmm, fe_type): members = [ ('x', types.int64), ('y', types.int64), ] models.StructModel.__init__(self, dmm, fe_type, members)

Data Layout

slide-8
SLIDE 8

8

④ Lower erin ing g calla llable bles, , se setattr tr, , ge getattr tr, …

from numba.extending import lower_builtin from numba import cgutils # llvm codegen utils @lower_builtin(MyPoint, types.Integer, types.Integer) def impl_point(context, builder, sig, args): typ = sig.return_type assert isinstance(typ, MyPointType) x, y = args point = cgutils.create_struct_proxy(typ)(context, builder) point.x = x point.y = y return point._getvalue()

Codegen utilities for StructModel Types of arguments

slide-9
SLIDE 9

9

In Integ egratio ation n wi with C/ h C/C+ C++

import numpy.ctypeslib from numba import carray, cfunc import cffi

slide-10
SLIDE 10

10

sys.exit(0)

References & Docs:

  • Good start: http://numba.pydata.org/numba-doc/latest/extending/interval-example.html
  • Numpy ctypeslib: https://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html
  • Numba uses Numba, so look at its code for examples:

https://github.com/numba/numba

  • Friendly introduction to LLVM: https://www.aosabook.org/en/llvm.html
  • Examples: https://fosdem.org/2019/schedule/event/python_extending_numba/
slide-11
SLIDE 11

Pho hotoni nics cs In Integ egrat ated ed Cir Circuit uit (P (PIC IC)? )?

slide-12
SLIDE 12

12

In Inter erop p wi with C+ h C++ + cla lasse sses? s?

  • 1. Write C – wrapper for your C++ code
  • 2. Create a Numba type and model to store a C++ obj pointer
  • 3. Implement attributes using Numba’s extension architecture
  • 4. Pass the pointer to the C++ object to your numba.cfunc
  • 5. Segfault ;-)