News for hackers. Stuff that matters.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by 490d0aff0ee8 on Tuesday November 05, 2019 @03:26PM (#21456299)
Tangent rant.

I'm skimming over some of the code at https://github.com/openai/gpt-2/blob/master/src/model.py and I can't help but feel frustrated at how unreadable this stuff is.

1. Why is it acceptable to have single-letter variable names everywhere?

2. There's little to almost no documentation in the code itself. It's unclear what the parameters of any given function mean.

3. There are magic constants everywhere.

4. Function names are so terse... ("gelu", "attn")

Re:OpenAI Releases Largest GPT-2 Text Generation Model by moultano on Tuesday November 05, 2019 @04:43PM (#21457038)
The notation in the code will be very familiar to anyone comfortable with the underlying research and math. The "conceptual" documentation is in the literature.

What you're asking for is the rough equivalent of asking a C programmer to name their loop variables "index" instead of "i." Everyone familiar with the concepts of c programming knows what "i" means in the context of a for loop. Similarly, everyone familiar with transformers knows what "gelu" and "attn" mean.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by randomsearch on Tuesday November 05, 2019 @06:14PM (#21457857)
This isn’t a good comparison. “i” is used domain independently across an entire language, not in some other domain. In fact, it’s used across the entirety of computer science (and originated in maths), so it’s across an entire discipline and even inter-disciplinary.

They should use proper variable names if they want to have the code understood by anyone non-specialist, and by people who use different terminology, and people looking back at the code in the future when terminology may have changed.

I don’t know about this domain, but the single-letter-variable name etc AKA “match the equation” is a curse when non-CS engineers/scientists write code. It often breaks code conventions, leaving IDEs to light up like a Christmas tree when opening the source. There’s a good reason CS moved from register letters to something closer to natural language.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by moultano on Tuesday November 05, 2019 @07:28PM (#21458520)
>I don’t know about this domain

So then why don't you believe me when I tell you that all of these variable names are extremely standard, and will be familiar to anyone who has written deep learning code before?

Re:OpenAI Releases Largest GPT-2 Text Generation Model by tommit on Wednesday November 06, 2019 @11:57AM (#21464402)
I feel like you both have good points. Yes, a lot of the variables are very ML specific and often called that way. However, I feel like that encourages the same researchers (who are obviously not software engineers) to give the rest of their variables sub-par names as well. Why would you give any variable a name longer than a word even, if so many you regularly encounter are just `w`, `u`, `x`, `hparam` ... and so on.

I'm a software engineer with a background in ML, so even though I somewhat know the domain language I still get mad at the blatant disrespect for PEP-8. That being said, this one is definitely one of the better codebases I have come across. This feels like it could be fairly easily worked with and understood. I have seen far, far worse code to go along research papers.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by high_derivative on Tuesday November 05, 2019 @03:36PM (#21456400)
My professional observation (as ml researcher at big tech):

These companies hire a lot of engineers straight out of undergrad/master's degrees. The interviews test leetcode knowledge, and today lots of degrees are heavy on Python-scripted ML homework.

The result is companies with billion dollar funding and world-changing goals having a lot of their code look like complete spaghetti.

And this is the engineers who are meant to clean up research scientist code. Scientists generally don't feel like it's their responsibility to write strong code.

Systems-side teams/orgs have better code, but essentially as soon as you enter the 'ml engineer/research engineer/research scientist' layer, it's doomed.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by gdb on Tuesday November 05, 2019 @04:14PM (#21456769)
(I work at OpenAI. Before that, I worked at Stripe. I've spent most of my software career thinking about how to build effective engineering cultures.)

I think this code is actually well-written and maintainable. This is proven in practice because we've adopted it many places in OpenAI, and I've personally found it very easy to adapt to other use-cases (certainly much more so than the from-scratch Transformer implementations I've written!).

As https://news.ycombinator.com/item?id=21456605 points out, the complexity of the code arises from the complexity of the underlying algorithm. Complexity due to software engineering concerns, like Tensorflow scopes, are elegantly handled. [edited for clarity:] Writing a Transformer in 174 lines of code requires a lot of deep thinking about the right underlying abstractions.

> but essentially as soon as you enter the 'ml engineer/research engineer/research scientist' layer, it's doomed.

We actually don't do this! Our only official technical title is "member of technical staff". (People sometimes choose to self-identify as an engineer or researcher, so you might see that on LinkedIn, but we don't have a distinction internally.) Everyone is responsible for their own code, and people care quite a bit about writing code that others can build on.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by high_derivative on Tuesday November 05, 2019 @04:33PM (#21456934)
Ok, since you took the time to respond, I just want to be constructive as well:

So I don't have a big problem with some of the function definitions which can be compact, as the other comment points out.

The reason I don't like this code is that it does not comment anything on the critical bits. I don't necessarily care about whether you call the input to your matmul 'x' or 'tensor' or 'input' (although consistency is nice).

The thing that would stop be from absorbing and modifying this code is that it does not comment on all the bits that are non obvious to me if I haven't written a Transformer before. For example:

'Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs.' - I will have to ask the colleague what that means. Why not write out what the actual issue is instead of mysteriously hinting at some potential problem?

Code like this "q, k, v = map(split_heads, tf.split(c, 3, axis=2))" will require me re-reading the paper section, then printing out all the tensors to think about what tensor would have which shape at which point. Instead of writing relatively useless linecomments like '#Transformer', I would comment all non-trivial shape modifications with the current layout, and what we are trying to achieve while modifying the layout.

The other issue of my original comment was not specifically on that codebase, but I am sure you would admit that the baselines code was pretty much exactly what I was writing about re: ml scripts. That's not to denigrate its incredible usefulness to the community.

Since you mentioned spinning up, I thought I would add a few comments on that as well:

I think the spinning up code base is good at making the code compact, and terrible at making sense of data flow for beginners. There are a lot of line comments, but they do not actually explain what is conceptually going on but often just repeat short-hands.

For example, look at the PPO implementation: https://github.com/openai/spinningup/blob/master/spinup/algo...

Here, the function is returning pi, logp, and log_p_pi (and v). Do you know how incredibly confusing the distinction between these is for beginners? In particular, there is no explanation why logp_pi even needs to be stored in the buffer.

We could recompute it from the states and stop the gradient when computing the likelihood ratio. A sensible tutorial-level comment here may be something along the lines of computing the likelihood in the same forward pass as computing the action, so we can later use it to compute the likelihood ratio. We could also later re-compute this from the buffered states.

I will stop here but I hope my point comes across, whenever I read code from your repos, there are some good parts (conciseness, cute numerical tricks) but there is as general missing sense of thoughtfulness on what the code is really trying to convey to a reader. It shows in the comments and it shows in the code organisation.

As a final note, I have seen this in many organisations and I do not mean to call you out. There is just this quality degradation that inevitably happens when nobody is incentivised (read: promoted, rewarded) to think about these things for an organisation.

Managers at all levels typically don't because they don't get close enough to the subtle issues on a day to day level. If you are lucky, you get senior individual contributors who still look at code and and raise the bar for the entire org. My genuine recommendation to you is to look for that, because a manager won't do that, and more fresh grads can't do it.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by jachiam on Tuesday November 05, 2019 @05:03PM (#21457235)
Hello! Spinning Up author here.

Very reasonable point that it is not clearly explained why you need to store logp_pi in the buffer. But the reason is that it would require additional code complexity to calculate it on the fly later. The likelihood ratio requires the denominator to be on the _old_ policy, so if you wanted to compute it on the fly, you would need to have a second policy in the computation graph to preserve the old policy while you change the current policy. You could not simply do a stop_gradient on the current policy and get the same results.

My personal feeling is that tutorial-style explanations like this don't fit nicely into code comment flow. As a result, most tutorial-style descriptions went into material on the Spinning Up website rather than into the code. It isn't 100% comprehensive, certainly, but RL has an enormous surface area (there are tons and tons of little details that teaching material could dive into) and I feel pretty good about what we were able to cover. :)

Re:OpenAI Releases Largest GPT-2 Text Generation Model by high_derivative on Tuesday November 05, 2019 @05:08PM (#21457275)
Thank you for responding. Well, my point is that in particular the gradient on the likelihood ratio is what trips people up. They ask questions like 'why is this ratio not always 1' or similar. This is why I would say explaining what is going where here is critical, i.e. that we save the prior logp_pi (even though we could recompute it) to treat it as a constant value when computing the ratio/the gradient. That would be, from my perspective, the key pedagogical moment of a PPO tutorial. However his is purely subjective and I agree that one can feel differently about where to put explanations.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by garmaine on Tuesday November 05, 2019 @04:17PM (#21456788)
I’m very sorry to see someone who obviously cares so much to be defending this code. This does not follow best practices, and using complexity of the underlying algorithm is just an excuse. Complex code can be beautiful and well documented.

Writing a complex method in 174 lines is not elegant nor beautiful. Writing a well documented file that can take an engineer in a different specialty and bring them up to speed in 1,000 lines is.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by WnZ39p0Dgydaz1 on Tuesday November 05, 2019 @04:35PM (#21456954)
No matter how much you comment your code, you are not going to bring people up to speed on an algorithm that requires background knowledge on dozens of scientific papers, hundreds of pages, with a few code comments. This code is aimed at researchers who are familiar with the techniques and have the necessary background knowledge. For such people, the code is very readable.

Think about it like this: If you write a game engine, are you going to document each function with proofs that explain how the underlying Physics works for people who don't have such knowledge? No, you assume that people who read game engine code have read some basic physics books.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by gdb on Tuesday November 05, 2019 @04:29PM (#21456899)
We also have code like that. For example, that's the explicit goal of the Spinning Up repo: https://github.com/openai/spinningup/blob/master/spinup/algo...

In practice, it's much harder to use that code, and we tend not to consume code like that internally. There's a real tradeoff!

Re:OpenAI Releases Largest GPT-2 Text Generation Model by falcor84 on Tuesday November 05, 2019 @04:44PM (#21457051)
ddpg() takes 17 parameters and is over 200 lines long. I'm very far from being a domain expert, but having worked in other complex domains, I'm pretty confident this can be redesigned such that it's both more maintainable and more pleasant to use.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by jachiam on Tuesday November 05, 2019 @05:21PM (#21457409)
Hello! Spinning Up author here. I would love to hear your thoughts on this! So far I have had a lot of success teaching people about DDPG using this code example, but I'm grateful for every fresh perspective. :)

Feel free to reach out by email, jachiam at openai.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by garmaine on Tuesday November 05, 2019 @07:24PM (#21458494)
There is no function in the world that should ever take 17 parameters. If the algorithm permits such configuration, as I am sure it does, then it should take a configuration object which has all these values. The object could then be constructed using special purpose factories that take fewer parameters, and then customized from there as needed.

It may be an indication that the whole thing needs refactoring though.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by matz1 on Tuesday November 05, 2019 @10:57PM (#21459609)
You refactor that way but then you make it unnecessarily more complicated.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by garmaine on Tuesday November 05, 2019 @04:32PM (#21456930)
Well then I’m sorry for that. It’s a good indicator of a broken culture.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by gerash on Tuesday November 05, 2019 @04:03PM (#21456664)
I don't know why you excluded PhDs but their code aren't any better necessarily. Hiring straight from school doesn't necessarily mean bad S/W engineering skills. But generally the research code is either made hastily during exploration or by people not having enough Software Engineering background.

Many research scientist I've seen at large tech companies don't have good CS background and write code that's either unreadable, unscalable, unmaintainable, or buggy at times. They often have good ideas but don't do much software design.

Having been both at infrastructure teams and research teams, there are certain individuals in research orgs joining straight from school who think they are responsible with coming with a new and sexy thing and other engineers are responsible to run in production. It's like computing eigenvectors in Matlab or Python over a toy dataset and thinking you've done the bulk of the work of computing PageRank in production and should receive all the credit for a full search engine.

That attitude is a red flag to me.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by high_derivative on Tuesday November 05, 2019 @04:05PM (#21456696)
I don't see how what you are saying is contradicting me. The point is that the scientists generally don't view it as their job, whereas the ml engineers/research engineers typically have it as their job to do the software architecture/engineering side.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by codingslave on Tuesday November 05, 2019 @04:08PM (#21456717)
Yeah I'm waiting for the backlash when companies realize this
Re:OpenAI Releases Largest GPT-2 Text Generation Model by WnZ39p0Dgydaz1 on Tuesday November 05, 2019 @04:17PM (#21456791)
I actually disagree with you here. I don't think the code is unreadable, it follows standard notation used in Machine Learning. If you read scientific papers you will notice that e.g. variable names are the same as those used in mathematical formulas that everyone in the field is familiar with. The same goes for parameters, function names, and so on. They are standard notation/naming and only look confusing to people outside of the ML field. Giving them long uncommon names would actually be more confusing.

As someone with experience in ML research I think this code is quite well written compared to what you typically see (a single function with hundreds of lines and dozens of if statements). I can immediately see what any of the functions does, and I haven't even read the paper.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by TTPrograms on Tuesday November 05, 2019 @03:57PM (#21456605)
It's a specification of essentially a complex graph of mathematical operations. If there's a function called

  def mult(a,b): return a*b
it's not much more informative to write:

  def mult(activation_a, activation_b): return activation_a*activation_b
Many of these functions are not much more complex than that, and the names along with their comments are more than sufficient given familiarity with the literature. If you think familiarity with the literature is unreasonable, it's still not clear what could improve code like this in reasonable space. "This is a linear function, which means that it satisfies f(x+a)=f(x)+f(a)"? "This is the attention head, it acts as a mask on the sequence input"? It would be like complaining that someone made a tree class and didn't put a comment explaining what a leaf node is. Code readability always assumes some reader context and minimum pre-existing knowledge (as do all forms of technical communication).
Re:OpenAI Releases Largest GPT-2 Text Generation Model by macawfish on Tuesday November 05, 2019 @08:37PM (#21458912)
When math is involved, it's much easier to read code with short variable and function names.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by make3 on Tuesday November 05, 2019 @04:17PM (#21456798)
I understand what you mean but please understand that this code is targeted at people which would at least have some background knowledge, like having read the seminal Transformer paper, "Attention Is All You Need", https://arxiv.org/abs/1706.03762

Most of the code becomes really straightforward once you have. A lot of the magic constants are the result of multi page proofs (like the GELU constant) that would be impractical to put in the code.

Deep learning research really is a field that requires some amount of knowledge, and it's normal that you don't automatically understand state of the art code. Here is the GPT2 paper https://d4mucfpksywv.cloudfront.net/better-language-models/l...

Re:OpenAI Releases Largest GPT-2 Text Generation Model by slimsag on Tuesday November 05, 2019 @03:35PM (#21456385)
In my experience, this is the norm in the ML scene. Giant globs of unreadable and in no way understandable code -- unless of course you already understand everything.

I think this is because the "product" so to speak is often the papers themselves, not the code, but I'm not sure.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by bredren on Tuesday November 05, 2019 @03:39PM (#21456426)
Could these functions just be implementations of math with matching variable names?
Re:OpenAI Releases Largest GPT-2 Text Generation Model by buboard on Tuesday November 05, 2019 @04:17PM (#21456793)
because a lot of it is meant to correspond to math equations so variables names like w, u, v , b ,g match the equations in the papers ? I actually think it's pretty readable, as long as you know what it i supposed to implement (i don't; but i imagine they are implementing a complex graph), and short names help figure out where things go in and out in one screenfull.

Complex graphs are literally a spaggeti of arrows, and this format actually is pretty readable (even though in pytorch it would be more readable). I guess they leave comments out because it's not really possible to understand each line on its own (unless it's an implementation detail); you have to read the paper to know what s going on

Re:OpenAI Releases Largest GPT-2 Text Generation Model by fermenflo on Tuesday November 05, 2019 @03:36PM (#21456391)
I agree, a lot of the code could be improved. But some of what you mentioned is fairly standard. Like "Gaussian Error Linear Units being GELU, w/b for weights/biases, etc...
Re:OpenAI Releases Largest GPT-2 Text Generation Model by jsinai on Tuesday November 05, 2019 @03:46PM (#21456498)
Not sure how standard that is ...
Re:OpenAI Releases Largest GPT-2 Text Generation Model by steve_musk on Tuesday November 05, 2019 @04:05PM (#21456683)
It’s very standard ML abbreviations.
Re:OpenAI Releases Largest GPT-2 Text Generation Model by m463 on Tuesday November 05, 2019 @05:00PM (#21457203)
Mirrors my thoughts regarding all math textbooks and published papers.

I remember reading a famous scientist (newton maybe) published a really accessible book on a subject, which was read by lots of lay persons and opened him up to lots of unwanted public attention.

So publishing in a more inscrutable way might be a way of assuring peer-to-peer communication.

Either that, or it's a labor of love where cleaning things up would detract from the forward momentum.

Re:OpenAI Releases Largest GPT-2 Text Generation Model by JoeMayoBot on Tuesday November 05, 2019 @03:59PM (#21456622)
Having worked with math/research folks in the past, this isn't surprising. That said, from a software engineering perspective, where a typical code review would identify this, it is immediately noticeable.