Skip to content

BUG: reference cycle in np.vectorize - #11977

Merged
seberg merged 5 commits into
numpy:masterfrom
mattip:vectorize-refcount
Jan 9, 2019
Merged

seberg merged 5 commits into
numpy:masterfrom
mattip:vectorize-refcount

Conversation

@mattip

@mattip mattip commented Sep 18, 2018 •

Copy link
Copy Markdown
Member

Issue #11867 demonstrated a leak when using np.vectorize on a bound class method in CPython 3. The test demonstrates the issue.

Still WIP since I have not found the root cause of the leak.

Edit: adding GC_Track to ufunc allows the garbage collector to break the cycle

@eric-wieser

Copy link
Copy Markdown
Member

My guess would be that the problem is np.ufunc not implementing a traverse method to tell the GC that it holds a cycle

@mattip mattip changed the title WIP, BUG: reference cycle in np.vectorize BUG: reference cycle in np.vectorize Sep 18, 2018
Comment thread numpy/core/src/umath/ufunc_object.c Resolved
Comment thread numpy/core/src/umath/ufunc_object.c Resolved
@mattip

mattip commented Sep 25, 2018

Copy link
Copy Markdown
Member Author

ping

Comment thread numpy/lib/tests/test_function_base.py Resolved
Comment thread numpy/lib/tests/test_function_base.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Have we worked out why this is the case? Am I right in thinking that this patch changes it from being unbreakable to just requiring a gc to break?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct. I do not really understand the root cause and why only in this specific case

@mattip

mattip commented Sep 25, 2018

Copy link
Copy Markdown
Member Author

Squashed to a single commit.

@eric-wieser

eric-wieser commented Sep 26, 2018 •

Copy link
Copy Markdown
Member

I think the commit message is wrong - you didn't break the reference cycle, you simply allowed the GC to find it (and break it on the python side)

Comment thread numpy/lib/tests/test_function_base.py Resolved
@eric-wieser

Copy link
Copy Markdown
Member

I wonder if you can use assert_no_gc_cycles here, which prints out the cycle upon failure, if possible

@mattip
mattip force-pushed the vectorize-refcount branch 2 times, most recently from a358d0d to 8f75986 Compare October 10, 2018 07:03
@mattip

mattip commented Oct 10, 2018

Copy link
Copy Markdown
Member Author

fixed first commit message, reworked test to focus on class.atrribute = frompyfunc(class.method) where the reference cycle is actually created

Comment thread numpy/lib/tests/test_function_base.py Resolved
@mattip
mattip force-pushed the vectorize-refcount branch from 8f75986 to 0794da8 Compare October 10, 2018 09:42
Comment thread numpy/lib/tests/test_function_base.py Resolved
@eric-wieser

Copy link
Copy Markdown
Member

Would be good to finish this up

@eric-wieser

eric-wieser commented Nov 10, 2018 •

Copy link
Copy Markdown
Member

tp_traverse will need minor changes to accommodate the extra reference added in #8955 - I suppose the PR of the two that doesn't go in first will need updating.

Fix will be as simple as:

if (ufunc->identity == PyUFunc_IdentityValue) {
    Py_VISIT(ufunc->identity_value);
}

Comment thread numpy/lib/tests/test_function_base.py Resolved
@mattip

mattip commented Nov 10, 2018

Copy link
Copy Markdown
Member Author

merging with master to resolve conflicts

Comment thread numpy/lib/tests/test_function_base.py Resolved
@mattip

mattip commented Nov 11, 2018

Copy link
Copy Markdown
Member Author

i will wait for #8955 and then rework this PR

@mattip
mattip force-pushed the vectorize-refcount branch 2 times, most recently from d1279ce to b2c7901 Compare December 4, 2018 07:18
@mattip
mattip force-pushed the vectorize-refcount branch from b2c7901 to b91a047 Compare December 4, 2018 12:03
@mattip
mattip force-pushed the vectorize-refcount branch from b91a047 to f73d96d Compare December 18, 2018 06:11
@mattip

mattip commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

rebased at git merge-base master maintenance/1.16.x. Would it be possible to get this into 1.16?

Comment thread numpy/core/src/umath/ufunc_object.c Resolved
Comment thread numpy/core/src/umath/ufunc_object.c Resolved
Comment thread numpy/lib/tests/test_function_base.py Resolved
Comment thread numpy/lib/tests/test_function_base.py Resolved
Comment thread numpy/lib/tests/test_function_base.py Outdated
return np.int(0)

def npy_bound(self, a):
return types.MethodType(np.frompyfunc(self.bound, 1, 1), a)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm very confused by what exactly we're trying to test here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

self.bound is part of a reference cycle that requires a gc collection cycle to break. I added more clarification in the test docstring. Improvements welcome

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PR looks good to me, if nobody complains will merge in a bit probably. The test couldbe a bit more obvious to read, or maybe add a trivial one:

def func(a):
    return 0
func.vectorized = np.frompyfunc(func)

giving a minimal cycle. The other part would probably be simpler by making it a realistic use case:

class A(object):
    def __init__(self):
        # Wrap bound_method into a ufunc with `np.frompyfunc`. This creates
        # a reference cycle:
        #     self.method -> self   # since it is a bound method (knows "self")
        #     self -> self.wrapped_method  # Set here
        #     self.wrapped_method -> self.method  # Stores self.method to call it
        self.wrapped_method = np.frompyfunc(self.method)

    def method(self):
        return 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I guess that breaks the parameterization. Although, I do not think you need the types.MethodType if the just do the self.bound_vectorized = np.frompyfunc(self.bound, 1, 1) before returning it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not following. Where would I add that line?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I think what confuses me is that I believe you do not need this at all. You can simply remove the npy_bound and npy_unbound logic and just make it a.f = np.frompyfunc(func, 1, 1).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Adopted

Comment thread numpy/core/src/umath/ufunc_object.c Resolved
Comment thread numpy/core/src/umath/ufunc_object.c Resolved
@mattip

mattip commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

should be squash-merged or I can squash and force-push

@charris

charris commented Dec 18, 2018

Copy link
Copy Markdown
Member

If you want it in 1.16, use the 1.16.1 milestone.

@mattip

mattip commented Dec 31, 2018

Copy link
Copy Markdown
Member Author

@eric-wieser ping

Comment thread numpy/core/src/umath/ufunc_object.c Resolved
Comment thread numpy/lib/tests/test_function_base.py Outdated
return np.int(0)

def npy_bound(self, a):
return types.MethodType(np.frompyfunc(self.bound, 1, 1), a)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I guess that breaks the parameterization. Although, I do not think you need the types.MethodType if the just do the self.bound_vectorized = np.frompyfunc(self.bound, 1, 1) before returning it?

@seberg

seberg commented Jan 9, 2019

Copy link
Copy Markdown
Member

Thanks @mattip lets put it in before we find something else to nitpick about ;).

@eric-wieser eric-wieser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Test now makes sense to me too - thanks for persisting

@seberg

seberg commented Jan 9, 2019

Copy link
Copy Markdown
Member

Cool, mac OS job is expected to fail. Next thing will be the object arrays ;).

@seberg
seberg merged commit f5b6850 into numpy:master Jan 9, 2019
@mattip
mattip deleted the vectorize-refcount branch May 20, 2019 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants