# How to pass jit\_options to jitclasses?

**URL:** <https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177>\
**Category:** Support: How do I do ...?\
**Created:** [August 16, 2020, 4:52am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177 "2020-08-16T04:52:30Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 16, 2020, 4:52am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/1 "2020-08-16T04:52:30Z")

</div>

Is this feature supported? Any workaround if not…

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 16, 2020, 5:59am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/2 "2020-08-16T05:59:09Z")

</div>

More specifically, I am interested in cache=True option to be passed as jitclass flag

---

<div class="post-metadata">

**Author:** ![Hannes](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/hannes/32/188_2.png) [@Hannes](https://numba.discourse.group/u/Hannes)\
**Post date:** [August 16, 2020, 10:54am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/3 "2020-08-16T10:54:21Z")

</div>

To my knowledge most compilations flags are not supported by jitclass yet (unless that has changed recently).

I had a similar issue witch caching, and in my case the workaround was relatively easy as the jitclass instances I had were actually constant. So I just turned it into a named tuple and made the methods normal jit-functions which can be cached (if you assure that the type of the named tuple is deterministic).

Maybe you can use the new mutuable StructRef that was released yesterday in 0.51, if you require a mutable data container

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 16, 2020, 11:12am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/4 "2020-08-16T11:12:14Z")

</div>

@Hannes I can’t use StructRef until [https://github.com/numba/numba/issues/6077](https://github.com/numba/numba/issues/6077) is fixed. My use case is a little bit more complex. I need both optionals and deferred types to continue with structs. I was able to compile with jitclasses and is working great so far but the compilation is taking way to long everytime you start the app

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [August 17, 2020, 12:58pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/5 "2020-08-17T12:58:34Z")

</div>

> To my knowledge most compilations flags are not supported by jitclass yet (unless that has changed recently).

This is correct as of Numba 0.51. `jitclass` does not support compilation flags directly.

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 17, 2020, 1:24pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/6 "2020-08-17T13:24:15Z")

</div>

@stuartarchibald  
This might be the workaround I was looking for:

```auto
from numba import typed, typeof, njit, int64, types
from numba.experimental import jitclass
from numba.extending import overload_method, overload

spec = [
    ('value', int64),
]

@jitclass(spec)
class Bag(object):
    def __init__ (self, value):
        self.value = value

mybag = Bag(21)

x = typed.List.empty_list(Bag.class_type.instance_type)
x.append(mybag)

@overload_method(types.misc.ClassInstanceType, 'clone', jit_options={'cache': True, 'parallel': True, 'nogil': True, 'boundscheck': False, 'inline': 'always'})
def ol_bag_clone(inst,):
    if inst is Bag.class_type.instance_type:
        def impl(inst,):
            return Bag(inst.value)
        return impl

@njit
def foo(z):
    t = z[0]
    c = t.clone()
    c.value = -21
    z.append(c)
    return z

l = foo(x)

```

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [August 17, 2020, 7:14pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/7 "2020-08-17T19:14:40Z")

</div>

@stuartarchibald, under such an approach, is it possible to separate the implementations for two different jitclasses?, ie

```auto
@overload_method(types.misc.ClassInstanceType, 'clone')
def ol_bag_clone(inst,):
    if inst is Bag.class_type.instance_type:
        def impl(inst,):
            return Bag(inst.value)
        return impl

@overload_method(types.misc.ClassInstanceType, 'clone')
def ol_bag_clone2(inst,):
    if inst is Bag2.class_type.instance_type:
        def impl(inst,):
            return 2
        return impl

```

I’m getting

```auto
No implementation of function Function(<function ol_bag_clone at 0x7f13d082d790>) found for signature:

 >>> ol_bag_clone(instance.jitclass.Bag2#7f13d055c640<value:int64>)

```

which suggests that the second implementation is being ignored.

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 17, 2020, 7:44pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/8 "2020-08-17T19:44:21Z")

</div>

@luk-f-a you should try this because you are overriding the same type

```auto
@overload_method(types.misc.ClassInstanceType, 'clone')
def ol_bag_clone(inst,):
    if inst is Bag.class_type.instance_type:
        def impl(inst,):
            return Bag(inst.value)
        return impl

    if inst is Bag2.class_type.instance_type:
        def impl(inst,):
            return 2
        return impl
```

---

<div class="post-metadata">

**Author:** ![Hannes](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/hannes/32/188_2.png) [@Hannes](https://numba.discourse.group/u/Hannes)\
**Post date:** [August 18, 2020, 7:45am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/9 "2020-08-18T07:45:40Z")

</div>

Neat! That may come in handy at some point 🙂

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [August 18, 2020, 8:02am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/10 "2020-08-18T08:02:47Z")

</div>

I’ve not thought about all the ins-and-outs of this yet, it’s another one for the “workarounds that may be ok” pile.

xref: [source of this pattern](https://github.com/numba/numba/issues/6032#issuecomment-674840597) so I don’t forget :)!

---

<div class="post-metadata">

**Author:** ![Hannes](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/hannes/32/188_2.png) [@Hannes](https://numba.discourse.group/u/Hannes)\
**Post date:** [August 18, 2020, 8:18am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/11 "2020-08-18T08:18:38Z")

</div>

Have you been able to confirm if the caching part is working?

I never really understood the philosophy of jitclass types and instance types which contain some (random?) numbers like in luk’s post

```auto
instance.jitclass.Bag2#7f13d055c640<value:int64>

```

Opening two interpreter sessions easily shows that these numbers are not constant between sessions, so I wonder if the caching machinery can handle this situation, or is this id simply ignored by numba’s type resolution?

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 18, 2020, 8:32am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/12 "2020-08-18T08:32:38Z")

</div>

@Hannes I don’t think is working… also deferred types suffer from the same problem from what I am seeing

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 18, 2020, 8:36am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/13 "2020-08-18T08:36:55Z")

</div>

@stuartarchibald not sure how stable this solution is, but it would be good if it is documented… also it would be great if the current/old jitclass implementation would be replaced with structrefs and overloaded methods.

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [August 18, 2020, 2:25pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/14 "2020-08-18T14:25:19Z")

</div>

yes, that would work, but I’m creating jitclasses dynamically (via factory function), so it’s impossible to apply that pattern.

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 18, 2020, 2:51pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/15 "2020-08-18T14:51:43Z")

</div>

@luk-f-a have you tried to dynamically create the function you want to overload, using python’s eval/exec/compile built-in functionality?

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [August 18, 2020, 3:25pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/16 "2020-08-18T15:25:01Z")

</div>

The function is already dynamically created, the problem is how to assign it to be the overload of a method of a jitclass (or in my case, a named tuple).

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 18, 2020, 4:15pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/17 "2020-08-18T16:15:26Z")

</div>

@luk-f-a Is this doing what you want?

```auto
dyn_func_str = """
def ol_bag_clone(inst,):
    if inst is Bag.class_type.instance_type:
        def impl(inst,):
            return Bag(-inst.value)
        return impl

    if inst is Bag2.class_type.instance_type:
        def impl(inst,):
            return 2.5
        return impl
"""

exec(dyn_func_str)
dyn_func_call = locals()['ol_bag_clone']
overload_method(types.misc.ClassInstanceType, 'clone')(dyn_func_call)

```

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [August 18, 2020, 9:15pm UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/18 "2020-08-18T21:15:00Z")

</div>

unfortunately no. Bag and Bag2 never exist together in the same namespace. They are created one by one as needed (think of a jitclass as a generic, Bag, Bag), and after they are created it’s necessary to overload a method one them. There’s never the opportunity to create only one method, they need to overload sequentially.

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 19, 2020, 11:43am UTC](https://numba.discourse.group/t/how-to-pass-jit-options-to-jitclasses/177/19 "2020-08-19T11:43:33Z")

</div>

@luk-f-a you can use global scope and I think you can overload dynamically the same method as many times as you want. only the last one will be use

```auto
exec(dyn_func_str, globals())
dyn_func_call = globals()['ol_bag_clone']
overload_method(types.misc.ClassInstanceType, 'clone')(dyn_func_call)
# second overload later on..
overload_method(types.misc.ClassInstanceType, 'clone')(new_ol_bag_clone)

```
