Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docker][Pylint] Use regexes for good names #11712

Closed

Conversation

AndrewZhaoLuo
Copy link
Contributor

@AndrewZhaoLuo AndrewZhaoLuo commented Jun 14, 2022

Now that we are cleaning up the test directory (see #11414), I thought this might be a good time to update pylint.

Depends on #11733

Furthermore we partially relax rules for variable names to allow most 1-2 lower case character names and 1 character upper case names (which are used a lot in the tests).

Motivation:

A lot of tests are like:

def test_multi_kernel():
    # graph
    n = tvm.runtime.convert(1024)
    A = te.placeholder((n,), name="A")
    B = te.placeholder((n,), name="B")
    C = te.compute(A.shape, lambda *i: A(*i) + B(*i), name="C")
    D = te.compute(A.shape, lambda *i: A(*i) + C(*i), name="D")
    s = te.create_schedule(D.op)
    # create iter var and assign them tags.
    px, x = s[C].split(C.op.axis[0], nparts=1)
    s[C].bind(px, te.thread_axis("pipeline"))
    px, x = s[D].split(D.op.axis[0], nparts=1)
    s[D].bind(px, te.thread_axis("pipeline"))

    # one line to build the function.
    def check_device(device, host="llvm"):
        if not tvm.testing.device_enabled(device):
            return
        dev = tvm.device(device, 0)
        fadd = tvm.build(s, [A, B, C, D], device, host, name="myadd")
        dev = tvm.device(device, 0)
        # launch the kernel.
        n = 1024
        a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), dev)
        b = tvm.nd.array(np.random.uniform(size=n).astype(B.dtype), dev)
        c = tvm.nd.array(np.random.uniform(size=n).astype(C.dtype), dev)
        d = tvm.nd.array(np.random.uniform(size=n).astype(D.dtype), dev)
        fadd(a, b, c, d)
        tvm.testing.assert_allclose(d.numpy(), a.numpy() * 2 + b.numpy(), rtol=1e-5)

Where capital letters represent TE placeholder tensors which seems reasonable to me.

@AndrewZhaoLuo
Copy link
Contributor Author

Will need to upgrade the CI images before merging this BTW.

Copy link
Member

@Mousius Mousius left a comment

Choose a reason for hiding this comment

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

Furthermore we partially relax rules for variable names to allow most 1-2 lower case character names and 1 character upper case names (which are used a lot in the tests).

Can we separate this out from this PR? The current tests are often hard to read because of this poor variable naming and that we should have a separate discussion as to whether we want to do this.

@AndrewZhaoLuo
Copy link
Contributor Author

Certainly: #11734.

Now re: discussion on changing variable names. Right now we allow the following whitelist of 'bad' variable names:

i,j,_,a,b,op,x,y,wd,lr,kv,k,v,s,p,h,c,m,n,X,t,g,f

I think the new regexes are simply an extension of the status quo (as if we allow 'a' and 'b' as variable names we should also allow 'd') but in a more readable format.

@AndrewZhaoLuo AndrewZhaoLuo changed the title [Docker][Pylint] Upgrade pylint, use regexes for good names [Docker][Pylint] Use regexes for good names Jun 15, 2022
@Mousius
Copy link
Member

Mousius commented Jun 16, 2022

@AndrewZhaoLuo I suggest we head in the direction of descriptive variable names and limit the single letter variables as much as we can, if we look at the C++ style guide it has a section detailing best practices for naming:
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules

For tests this is extra important, as the single letter variables make it harder to follow the logic of the test. For example, why is A the placeholder for a rather than defining placeholder_a and array_a to clarify that relationship?

@AndrewZhaoLuo
Copy link
Contributor Author

@AndrewZhaoLuo I suggest we head in the direction of descriptive variable names and limit the single letter variables as much as we can, if we look at the C++ style guide it has a section detailing best practices for naming: https://google.github.io/styleguide/cppguide.html#General_Naming_Rules

For tests this is extra important, as the single letter variables make it harder to follow the logic of the test. For example, why is A the placeholder for a rather than defining placeholder_a and array_a to clarify that relationship?

I agree with you in principle, however as it stands we do allow 'bad' variable names, however they are limited to a very small list which is fairly arbitrary. It seems to me it was basically made in order for the linter to pass.

We can always restrict things later, but as it stands right now it provides an inconsistent policy which makes #11414 a bit harder to do.

@quic-sanirudh
Copy link
Contributor

quic-sanirudh commented Jul 20, 2022

Is there any ideas to either merge this patch, or modify this to remove all the single variable names altogether. When modifying the pytests to be linter compliant, I normally just run the linter and fix the errors it reports (I guess others do it in a similar fashion).

As it stands now, the linter errors for variable z but not for x and y, so we might end up missing a few bad variable names. Right now, I can manually change the linter regex in my local machine to avoid missing out on the bad variable names, but it might be good if we decided to go one way or another.

@AndrewZhaoLuo
Copy link
Contributor Author

@quic-sanirudh I haven't found time to get to this. I think your plan of closing the loophole is a good one. However, it is another major refactor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants