fvutils / pyvsc

Python packages providing a library for Verification Stimulus and Coverage
https://fvutils.github.io/pyvsc
Apache License 2.0
113 stars 26 forks source link

@vsc.covergroup doesn't work in Jupyter #115

Closed qzcx closed 3 years ago

qzcx commented 3 years ago

I like to work in jupyter notebooks when working on examples, creating graphs or examining data. A skill I picked up in multiple machine learning classes.

While trying msmftc's example in #107, I noticed that covergroups classes fail to create. Giving the following error:

TypeError: <module '__main__'> is a built-in class
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-5b7b941080bd> in <module>
     20 
     21 
---> 22 @vsc.covergroup
     23 class BranchInstr_cg:
     24     def __init__(self):

python36_venv/lib64/python3.6/site-packages/vsc/coverage.py in covergroup(T)
    270 
    271     # Store declaration information on the type
--> 272     file = inspect.getsourcefile(T)
    273     lineno = inspect.getsourcelines(T)[1]
    274     setattr(T, "_srcinfo_decl", SourceInfo(file, lineno))

/usr/lib64/python3.6/inspect.py in getsourcefile(object)
    682     Return None if no way can be identified to get the source.
    683     """
--> 684     filename = getfile(object)
    685     all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
    686     all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]

/usr/lib64/python3.6/inspect.py in getfile(object)
    652             if hasattr(object, '__file__'):
    653                 return object.__file__
--> 654         raise TypeError('{!r} is a built-in class'.format(object))
    655     if ismethod(object):
    656         object = object.__func__

TypeError: <module '__main__'> is a built-in class

It seems to be an issue with trying to get a source file, but likely it is confused since a jupyter notebook is more of an extention of an ipython terminal instead of running a python file directly. Overall, this is only a minor issue since randobj and constraints seem to work fine in jupyter notebooks and I can just graph a histogram of the results instead of generating coverage.

Here is the code from #107 which I used:

import vsc

@vsc.randobj
class BranchInstr:
    def __init__(self):
        self.type = vsc.rand_bit_t(1)
        self.disp = vsc.rand_bit_t(22)

    @vsc.constraint
    def short_offset_cnstr(self):
        with vsc.if_then(self.type == 0):
            self.disp < 4096
        with vsc.else_then:
            self.disp >= 8192
            self.disp < 16384

    def __str__(self):
        return(f"type = {self.type}, displacement = {self.disp}")

@vsc.covergroup
class BranchInstr_cg:
    def __init__(self):
        self.with_sample(item = BranchInstr())

        self.type = vsc.coverpoint(self.item.type)

        self.disp_cp = vsc.coverpoint(self.item.disp, bins = {
            "type0_disp" : vsc.bin_array([16], [0, 4095]),
            "type1_disp" : vsc.bin_array([32], [8192, 16383])
        })

branchInstr = BranchInstr()
branchInstr_cg = BranchInstr_cg()

for i in range(1024):
    branchInstr.randomize()
    branchInstr_cg.sample(branchInstr)
    # print(branchInstr)

vsc.report_coverage(details=True)
mballance commented 3 years ago

Thanks for pointing this out, @qzcx! I haven't done much with Jupyter notebooks, but the error does make sense in the context of something that doesn't "really" have source information. Let me look into this a bit, but certainly worth fixing for completeness.

qzcx commented 3 years ago

Thanks.

I hit the issue above using visual studio code with the Jupyter extension.

I also tried to repro the issue in google colab (google's free cloud jupyter notebook tool), but had issues installing pyvsc. So I'd recommend trying it in a local server instance.

I was also able to repro the issue just using the normal python terminal which is a bit painful to plug in, but is at least another data point.

>python
>>> import vsc
>>> @vsc.randobj
... class BranchInstr:
...   def __init__(self):
...     self.type = vsc.rand_bit_t(1)
...     self.disp = vsc.rand_bit_t(22)
... 
>>> @vsc.covergroup
... class BranchInstr_cg:
...   def __init__(self):
...     self.with_sample(item = BranchInstr())
...     self.type = vsc.coverpoint(self.item.type)
...     self.disp_cp = vsc.coverpoint(self.item.disp, bins = {"type0_disp" : vsc.bin_array([16], [0, 4095]),"type1_disp" : vsc.bin_array([32], [8192, 16383]) })
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python36_venv/lib/python3.6/site-packages/vsc/coverage.py", line 272, in covergroup
    file = inspect.getsourcefile(T)
  File "python/3.6.8/lib/python3.6/inspect.py", line 684, in getsourcefile
    filename = getfile(object)
  File "3.6.8/lib/python3.6/inspect.py", line 654, in getfile
    raise TypeError('{!r} is a built-in class'.format(object))
TypeError: <module '__main__' (built-in)> is a built-in class
aneels3 commented 3 years ago

Hi @qzcx I can see you are using Python 3.6.8. Try installing Python version 3.7+ (If possible) Just a guess, it might work.

Regards, Anil

mballance commented 3 years ago

Hi Jon (@qzcx), I've corrected this in the 0.6.0 release.