← Lex Fridman

Chris Lattner: Compilers, LLVM, Swift, TPU, and ML Accelerators

第2019-05-13期2019-05-13English

嘉宾:Chris Lattner

收听本集00:00 / 1:13:15
阅读设置
按你的习惯阅读
正文字号
下载播客精读
把这一篇,带走慢慢读

精读待补充

下载功能还未开放

§01Transcript · 00:00–07:59

00:00The following is a conversation with Chris Latner.

00:02Currently, he's a senior director at Google,

00:05working on several projects,

00:06including CPU, GPU, TPU accelerators for TensorFlow,

00:10Swift for TensorFlow,

00:12and all kinds of machine learning compiler magic

00:14going on behind the scenes.

00:16He's one of the top experts in the world

00:18on compiler technologies,

00:20which means he deeply understands the intricacies

00:23of how hardware and software come together

00:26to create efficient code.

00:27He created the LLVM compiler infrastructure project

00:31and the Clang compiler.

00:33He led major engineering efforts at Apple,

00:36including the creation of the Swift programming language.

00:38He also briefly spent time at Tesla

00:41as vice president of Autopilot software

00:44during the transition from Autopilot hardware one

00:46to hardware two,

00:48when Tesla essentially started from scratch

00:51to build an in-house software infrastructure for Autopilot.

00:54I could have easily talked to Chris for many more hours.

00:58Compiling code down across the levels of abstraction

01:01is one of the most fundamental and fascinating aspects

01:04of what computers do.

01:05And he is one of the world experts in this process.

01:08It's rigorous science and it's messy, beautiful art.

01:12This conversation is part

01:14of the Artificial Intelligence podcast.

01:16If you enjoy it, subscribe on YouTube, iTunes,

01:19or simply connect with me on Twitter @LexFriedman,

01:22spelled F-R-I-D.

01:24And now here's my conversation with Chris Ladner.

01:29What was the first program you've ever written?

01:32- My first program?

01:34- Back, and when was it?

01:35- I think I started as a kid

01:37and my parents got a basic programming book.

01:41And so when I started,

01:42it was typing out programs from a book

01:45and seeing how they worked and then typing them in wrong

01:49and trying to figure out why they were not working right,

01:51that kind of stuff.

01:53- So basic, what was the first language

01:54that you remember yourself maybe falling in love with,

01:58like really connecting with?

02:00- I don't know, I mean, I feel like I've learned a lot

02:01along the way and each of them

02:03have a different special thing about them.

02:06So I started in basic and then went like GW basic,

02:09which was the thing back in the DOS days,

02:11and then upgraded to Q basic and eventually Quick basic,

02:15which are all slightly more fancy versions

02:17of Microsoft basic.

02:19Made the jump to Pascal

02:20and started doing machine language programming

02:23and assembly in Pascal, which was really cool.

02:25Turbo Pascal was amazing for its day.

02:28Eventually got into C, C++

02:29and then kind of did lots of other weird things.

02:33- I feel like you took the dark path,

02:34which is the, you could have gone Lisp.

02:39- Yeah, yeah.

02:40- You could have gone higher level

02:41sort of functional philosophical hippie route.

02:44Instead you went into like the dark arts of the C.

02:48- It was straight into the machine.

02:49- Straight to the machine.

02:50So it started with basic Pascal and then assembly

02:53and then wrote a lot of assembly.

02:55And I eventually did small talk

02:58and other things like that,

03:00but that was not the starting point.

03:01- But so what is this journey into C?

03:05Is that in high school?

03:06Is that in college?

03:07- That was in high school, yeah.

03:08So, and then that was really about

03:13trying to be able to do more powerful things

03:15than what Pascal could do

03:16and also to learn a different world.

03:18C was really confusing to me with pointers

03:20and the syntax and everything,

03:21and it took a while,

03:22but Pascal's much more principled in various ways.

03:27C is more, I mean, it has its historical roots,

03:33but it's not as easy to learn.

03:35- With pointers, there's this memory management thing

03:39that you have to become conscious of.

03:41Is that the first time you start to understand

03:43that there's resources that you're supposed to manage?

03:46- Well, so you have that in Pascal as well,

03:48but in Pascal, these, like the caret instead of the star,

03:51and there's some small differences like that,

03:53but it's not about pointer arithmetic.

03:55And in C, you end up thinking about

03:58how things get laid out in memory a lot more.

04:00And so in Pascal, you have allocating and deallocating

04:04and owning the memory,

04:05but just the programs are simpler

04:07and you don't have to,

04:10well, for example, Pascal has a string type.

04:12And so you can think about a string

04:14instead of an array of characters

04:15which are consecutive in memory.

04:17So it's a little bit of a higher level abstraction.

04:20- So let's get into it.

04:22Let's talk about LLVM, Clang, and compilers.

04:25- Sure.

04:26- So can you tell me first what LLVM and Clang are

04:31and how is it that you find yourself

04:33the creator and lead developer,

04:35one of the most powerful compiler optimization systems

04:39in use today?

04:40- Sure, so I guess they're different things.

04:43So let's start with what is a compiler?

04:47It's a--

04:47- Is that a good place to start?

04:48- What are the phases of a compiler?

04:50Where are the parts?

04:51Yeah, what is it?

04:51- So what is even a compiler used for?

04:53So the way I look at this is you have a two-sided problem

04:57of you have humans that need to write code

05:00and then you have machines that need to run the program

05:02that the human wrote.

05:03And for lots of reasons,

05:04the humans don't wanna be writing in binary

05:07and wanna think about every piece of hardware.

05:08And so at the same time that you have lots of humans,

05:12you also have lots of kinds of hardware.

05:14And so compilers are the art of allowing humans

05:17to think at a level of abstraction

05:19that they wanna think about and then get that program,

05:22get the thing that they wrote

05:23to run on a specific piece of hardware.

05:26And the interesting and exciting part of all this

05:29is that there's now lots of different kinds of hardware,

05:31chips like x86 and PowerPC and ARM and things like that,

05:35but also high-performance accelerators

05:37for machine learning and other things like that,

05:38or also just different kinds of hardware, GPUs.

05:41These are new kinds of hardware.

05:42And at the same time on the programming side of it,

05:45you have your basic, you have C, you have JavaScript,

05:48you have Python, you have Swift,

05:50you have like lots of other languages

05:52that are all trying to talk to the human in a different way

05:55to make them more expressive and capable and powerful.

05:58And so compilers are the thing

06:01that goes from one to the other.

06:03- End to end, from the very beginning to the very end.

06:05- End to end.

06:06And so you go from what the human wrote

06:08and programming languages end up being about

06:11expressing intent, not just for the compiler

06:14and the hardware, but the programming language's job

06:17is really to capture an expression

06:20of what the programmer wanted

06:22that then can be maintained and adapted

06:25and evolved by other humans,

06:27as well as interpreted by the compiler.

06:29So when you look at this problem,

06:31you have on the one hand humans, which are complicated,

06:34and you have hardware, which is complicated.

06:36And so compilers typically work in multiple phases.

06:39And so the software engineering challenge

06:42that you have here is try to get maximum reuse

06:44out of the amount of code that you write,

06:47because these compilers are very complicated.

06:49And so the way it typically works out

06:51is that you have something called a front end or a parser

06:54that is language specific.

06:56And so you'll have a C parser, and that's what Clang is,

06:59or C++ or JavaScript or Python or whatever,

07:03that's the front end.

07:04Then you'll have a middle part,

07:07which is often the optimizer.

07:08And then you'll have a late part,

07:11which is hardware specific.

07:13And so compilers end up,

07:15there's many different layers often,

07:16but these three big groups are very common in compilers.

07:20And what LLVM is trying to do

07:22is trying to standardize that middle and last part.

07:25And so one of the cool things about LLVM

07:27is that there are a lot of different languages

07:29that compile through to it.

07:31And so things like Swift, but also Julia, Rust,

07:36Clang for C, C++, Objective-C,

07:39like these are all very different languages

07:40and they can all use the same optimization infrastructure,

07:43which gets better performance,

07:45and the same code generation infrastructure

07:47for hardware support.

07:48And so LLVM is really that layer that is common

07:52that all these different specific compilers can use.

07:55- And is it a standard, like a specification,

07:59or is it literally an implementation?

§02Transcript · 08:01–15:53

08:01- It's an implementation.

08:02And so I think there's a couple of different ways

08:05of looking at it, right?

08:06Because it depends on which angle you're looking at it from.

08:09LLVM ends up being a bunch of code, okay?

08:12So it's a bunch of code that people reuse

08:14and they build compilers with.

08:16We call it a compiler infrastructure

08:18because it's kind of the underlying platform

08:20that you build a concrete compiler on top of.

08:22But it's also a community.

08:23And the LLVM community is hundreds of people

08:26that all collaborate.

08:27And one of the most fascinating things about LLVM

08:30over the course of time is that we've managed somehow

08:34to successfully get harsh competitors

08:37in the commercial space to collaborate

08:39on shared infrastructure.

08:41And so you have Google and Apple, you have AMD and Intel,

08:45you have Nvidia and AMD on the graphics side,

08:48you have Cray and everybody else doing these things.

08:52And like all these companies are collaborating together

08:55to make that shared infrastructure really, really great.

08:58And they do this not out of the goodness of their heart,

09:01but they do it because it's in their commercial interest

09:03of having really great infrastructure

09:05that they can build on top of,

09:06and facing the reality that it's so expensive

09:09that no one company, even the big companies,

09:11no one company really wants to implement it all themselves.

09:14- Expensive or difficult?

09:16- Both.

09:16That's a great point because it's also about the skill sets.

09:20- Right.

09:20- And the skill sets are very hard to find.

09:25- How big is the LLVM?

09:28It always seems like with open source projects,

09:30the kind, and LLVM is open source?

09:33- Yes, it's open source.

09:34It's about, it's 19 years old now, so it's fairly old.

09:38- It seems like the magic often happens

09:40in a very small circle of people.

09:43- Yes.

09:43- At least at early birth and whatever.

09:46- Yes.

09:46So LLVM came from a university project,

09:49and so I was at the University of Illinois,

09:51and there it was myself, my advisor,

09:53and then a team of two or three research students

09:57in the research group,

09:58and we built many of the core pieces initially.

10:02I then graduated and went to Apple,

10:03and at Apple brought it to the products,

10:06first in the OpenGL graphics stack,

10:09but eventually to the C compiler realm,

10:11and eventually built Clang,

10:12and eventually built Swift and these things,

10:14along the way building a team of people

10:16that are really amazing compiler engineers

10:18that helped build a lot of that.

10:20And so as it was gaining momentum,

10:21and as Apple was using it, being open source and public,

10:24and encouraging contribution,

10:26many others, for example, at Google,

10:28came in and started contributing,

10:30and in some cases, Google effectively owns Clang now,

10:33because it cares so much about C++

10:35and the evolution of that ecosystem,

10:37and so it's investing a lot in the C++ world

10:41and the tooling and things like that.

10:42And so likewise, NVIDIA cares a lot about CUDA,

10:47and so CUDA uses Clang and uses LLVM

10:50for graphics and GPU.

10:54- And so when you first started as a master's project,

10:58I guess, did you think it was gonna go as far as it went?

11:02Were you crazy ambitious about it?

11:06- No. - It seems like

11:07a really difficult undertaking, a brave one.

11:09- Yeah, no, no, no, it was nothing like that.

11:11So I mean, my goal when I went to the University of Illinois

11:13was to get in and out with a non-thesis master's in a year,

11:17and get back to work.

11:18So I was not planning to stay for five years

11:22and build this massive infrastructure.

11:24I got nerd sniped into staying,

11:27and a lot of it was because LLVM was fun,

11:29I was building cool stuff

11:30and learning really interesting things

11:33and facing both software engineering challenges,

11:36but also learning how to work in a team

11:38and things like that.

11:40I had worked at many companies as interns before that,

11:43but it was really a different thing

11:45to have a team of people that are working together

11:48and trying to collaborate in version control,

11:50and it was just a little bit different.

11:52- Like I said, I just talked to Don Knuth,

11:54and he believes that 2% of the world population

11:56have something weird with their brain,

11:58that they're geeks, they understand computers,

12:01they're connected with computers.

12:02He put it at exactly 2%.

12:04Okay, so--

12:05- He's a specific guy.

12:06- It's very specific.

12:08What he says is, I can't prove it,

12:10but it's very empirically there.

12:11Is there something that attracts you

12:14to the idea of optimizing code?

12:16I mean, it seems like that's one of the biggest,

12:19coolest things about LLVM.

12:20- Yeah, that's one of the major things it does.

12:22So I got into that because of a person, actually.

12:26So when I was in my undergraduate,

12:28I had an advisor, or a professor named Steve Vegdal,

12:32and I went to this little, tiny, private school.

12:35There were, I think, seven or nine people

12:38in my computer science department,

12:40students in my class.

12:43So it was a very tiny, very small school.

12:47It was kind of a wart on the side of the math department,

12:49kind of a thing at the time.

12:51I think it's evolved a lot in the many years since then.

12:53But Steve Vegdal was a compiler guy,

12:58and he was super passionate.

12:59And his passion rubbed off on me,

13:02and one of the things I like about compilers

13:04is that they're large, complicated software pieces.

13:09And so one of the culminating classes

13:12that many computer science departments,

13:14at least at the time, did was to say

13:16that you would take algorithms and data structures

13:18in all these core classes,

13:19but then the compilers class

13:20was one of the last classes you take,

13:22because it pulls everything together,

13:24and then you work on one piece of code

13:26over the entire semester.

13:28And so you keep building on your own work,

13:32which is really interesting.

13:33It's also very challenging,

13:34because in many classes, if you don't get a project done,

13:37you just forget about it and move on to the next one

13:39and get your B or whatever it is.

13:41But here you have to live with the decisions you make

13:43and continue to reinvest in it,

13:45and I really like that.

13:46And so I did a extra study project with him

13:51the following semester, and he was just really great.

13:53And he was also a great mentor in a lot of ways.

13:56And so from him and from his advice,

13:59he encouraged me to go to graduate school.

14:01I wasn't super excited about going to grad school.

14:03I wanted the master's degree,

14:05but I didn't want to be an academic.

14:07But like I said, I kind of got tricked into saying

14:11I was having a lot of fun,

14:12and I definitely do not regret it.

14:14- What aspects of compilers

14:15were the things you connected with?

14:17So LLVM, there's also the other part

14:22that's just really interesting,

14:23if you're interested in languages,

14:24is parsing and just analyzing,

14:27like, yeah, analyzing the language,

14:29breaking it down, parsing, and so on.

14:31Was that interesting to you,

14:32or were you more interested in optimization?

14:34- For me, it was more, so I'm not really a math person.

14:37I can do math.

14:38I understand some bits of it when I get into it,

14:41but math is never the thing that attracted me.

14:43And so a lot of the parser part of the compiler

14:46has a lot of good formal theories

14:47that Don, for example, knows quite well.

14:50Still waiting for his book on that.

14:52But I just like building a thing

14:56and seeing what it could do,

14:57and exploring and getting it to do more things,

15:00and then setting new goals and reaching for them.

15:03And in the case of LLVM, when I started working on that,

15:09my research advisor that I was working for

15:12was a compiler guy.

15:13And so he and I specifically found each other

15:15because we were both interested in compilers,

15:16and so I started working with him and taking his class.

15:19And a lot of LLVM initially was,

15:21it's fun implementing all the standard algorithms

15:23and all the things that people had been talking about

15:26and were well-known and they were in the curricula

15:28for advanced studies in compilers.

15:31And so just being able to build that was really fun.

15:34And I was learning a lot by,

15:36instead of reading about it, just building.

15:38And so I enjoyed that.

15:40- So you said compilers are these complicated systems.

15:42Can you even just, with language, try to describe

15:47how you turn a C++ program into code?

15:52Like what are the hard parts?

15:53Why is it so hard?

§03Transcript · 15:54–23:58

15:54- So I'll give you examples of the hard parts

15:56along the way.

15:57So C++ is a very complicated programming language.

16:01It's something like 1,400 pages in the spec.

16:03So C++ by itself is crazy complicated.

16:06- Can we just, sorry, pause.

16:07What makes the language complicated

16:08in terms of what's syntactically?

16:12- So it's what they call syntax.

16:14So the actual, how the characters are arranged, yes.

16:16It's also semantics, how it behaves.

16:20It's also, in the case of C++,

16:21there's a huge amount of history.

16:23C++ built on top of C.

16:25You play that forward and then a bunch of suboptimal,

16:28in some cases, decisions were made and they compound.

16:31And then more and more and more things

16:33keep getting added to C++ and it will probably never stop.

16:37But the language is very complicated from that perspective.

16:39And so the interactions between subsystems

16:41is very complicated.

16:42There's just a lot there.

16:43And when you talk about the front end,

16:45one of the major challenges, which Clang, as a project,

16:48the C++ compiler that I built, I and many people built,

16:52one of the challenges we took on was we looked at GCC.

16:57Okay, GCC at the time was like a really good

17:01industry standardized compiler that had really consolidated

17:05a lot of the other compilers in the world

17:06and was a standard.

17:08But it wasn't really great for research.

17:10The design was very difficult to work with.

17:12And it was full of global variables and other things

17:16that made it very difficult to reuse

17:18in ways that it wasn't originally designed for.

17:20And so with Clang, one of the things that we wanted to do

17:22is push forward on better user interface.

17:25So make error messages that are just better than GCCs.

17:28And that's actually hard because you have to do

17:29a lot of bookkeeping in an efficient way

17:31to be able to do that.

17:33We wanted to make compile time better.

17:35And so compile time is about making it efficient,

17:37which is also really hard when you're keeping track

17:39of extra information.

17:40We wanted to make new tools available.

17:43So refactoring tools and other analysis tools

17:46that the GCC never supported,

17:48also leveraging the extra information we kept,

17:51but enabling those new classes of tools

17:54that then get built into IDEs.

17:55And so that's been one of the areas that Clang

17:58has really helped push the world forward in,

18:01is in the tooling for C and C++ and things like that.

18:05But C++ in the front end piece is complicated

18:07and you have to build syntax trees

18:08and you have to check every rule in the spec

18:11and you have to turn that back into an error message

18:13to the human that the human can understand

18:16when they do something wrong.

18:17But then you start doing the, what's called lowering.

18:20So going from C++ and the way that it represents code

18:23down to the machine.

18:24And when you do that,

18:25there's many different phases you go through.

18:28Often there are, I think LLVM has something like 150

18:33different, what are called passes in the compiler

18:36that the code passes through.

18:38And these get organized in very complicated ways,

18:41which affect the generated code and the performance

18:44and compile time and many other things.

18:45- What are they passing through?

18:47So after you do the Clang parsing,

18:51what's the, is it a graph?

18:53What does it look like?

18:54What's the data structure here?

18:55- Yeah, so in the parser, it's usually a tree

18:59and it's called an abstract syntax tree.

19:01And so the idea is you have a node for the plus

19:04that the human wrote in their code or the function call,

19:07you'll have a node for call with the function that they call

19:10and the arguments they pass, things like that.

19:13This then gets lowered into what's called

19:16an intermediate representation.

19:18And intermediate representations are like LLVM has one.

19:22And there it's a, it's what's called a control flow graph.

19:26And so you represent each operation in the program

19:31as a very simple, like this is gonna add two numbers,

19:34this is gonna multiply two things, maybe we'll do a call,

19:37but then they get put in what are called blocks.

19:40And so you get blocks of these straight line operations,

19:43where instead of being nested like in a tree,

19:45it's straight line operations.

19:46And so there's a sequence and an ordering

19:48to these operations.

19:49- So within the block or outside the block?

19:51- That's within the block.

19:53And so it's a straight line sequence of operations

19:54within the block, and then you have branches,

19:57like conditional branches between blocks.

20:00And so when you write a loop, for example,

20:02in a syntax tree, you would have a for node,

20:07like for a for statement in a C-like language,

20:09you'd have a for node,

20:10and you have a pointer to the expression

20:12for the initializer, a pointer to the expression

20:14for the increment, a pointer to the expression

20:15for the comparison, a pointer to the body.

20:18Okay, and these are all nested underneath it.

20:21In a control flow graph, you get a block for the code

20:24that runs before the loop, so the initializer code,

20:27then you have a block for the body of the loop,

20:30and so the body of the loop code goes in there,

20:33but also the increment and other things like that,

20:35and then you have a branch that goes back to the top,

20:37and a comparison and a branch that goes out.

20:39And so it's more of a assembly level kind of representation.

20:44But the nice thing about this level of representation

20:46is it's much more language independent.

20:48And so there's lots of different kinds of languages

20:51with different kinds of, you know, JavaScript

20:55has a lot of different ideas of what is false, for example,

20:58and all that can stay in the front end,

21:00but then that middle part can be shared across all those.

21:04- How close is that intermediate representation

21:07to neural networks, for example?

21:10Is there, are they, 'cause everything you describe

21:13is a kind of echoes of a neural network graph.

21:16Are they neighbors, or what?

21:18- They're quite different in details,

21:20but they're very similar in idea.

21:22So one of the things that neural networks do

21:24is they learn representations for data

21:26at different levels of abstraction, right?

21:29And then they transform those through layers, right?

21:32So the compiler does very similar things,

21:35but one of the things the compiler does

21:37is it has relatively few different representations.

21:40Where a neural network often, as you get deeper,

21:42for example, you get many different representations,

21:44and each, you know, layer or set of ops

21:47is transforming between these different representations.

21:50In a compiler, often you get one representation

21:53and they do many transformations to it.

21:55And these transformations are often applied iteratively.

21:58And for programmers, there's familiar types of things.

22:02For example, trying to find expressions inside of a loop

22:06and pulling them out of a loop,

22:07so that they execute for your times,

22:08or find redundant computation,

22:10or find constant folding or other simplifications,

22:15turning, you know, two times X into X shift left by one,

22:19and things like this are all the examples

22:21of the things that happen.

22:23But compilers end up getting a lot of theorem proving

22:26and other kinds of algorithms

22:27that try to find higher level properties of the program

22:29that then can be used by the optimizer.

22:32- Cool, so what's like the biggest bang for the buck

22:35with optimization?

22:37What's a-- - Today?

22:38Yeah.

22:39- Well, no, not even today.

22:40At the very beginning, the '80s, I don't know.

22:42- Oh yeah, so for the '80s,

22:43a lot of it was things like register allocation.

22:46So the idea of in a modern, like a microprocessor,

22:50what you'll end up having is you'll end up having memory,

22:52which is relatively slow,

22:54and then you have registers that are relatively fast.

22:57But registers, you don't have very many of them, okay?

23:00And so when you're writing a bunch of code,

23:02you're just saying like, compute this,

23:04put in a temporary variable, compute this,

23:05compute this, compute this, put in a temporary variable,

23:07I have a loop, I have some other stuff going on.

23:09Well, now you're running on an x86,

23:11like a desktop PC or something.

23:13Well, it only has, in some cases,

23:16some modes, eight registers, right?

23:18And so now the compiler has to choose

23:20what values get put in what registers

23:22at what points in the program.

23:24And this is actually a really big deal.

23:26So if you think about, you have a loop,

23:28an inner loop that executes millions of times maybe.

23:31If you're doing loads and stores inside that loop,

23:33then it's gonna be really slow.

23:34But if you can somehow fit all the values

23:37inside that loop in registers, now it's really fast.

23:40And so getting that right requires a lot of work

23:43because there's many different ways to do that.

23:44And often what the compiler ends up doing

23:46is it ends up thinking about things

23:48in a different representation than what the human wrote.

23:51Right, you wrote into x.

23:53Well, the compiler thinks about that

23:54as four different values,

23:56each which have different lifetimes

23:58across the function that it's in.

§04Transcript · 24:00–31:38

24:00And each of those could be put in a register

24:02or memory or different memory,

24:04or maybe in some parts of the code,

24:06recomputed instead of stored and reloaded.

24:08And there are many of these different kinds of techniques

24:10that can be used.

24:11- So it's adding almost like a time dimension to,

24:15it's trying to optimize across time.

24:18So it's considering when you're programming,

24:20you're not thinking in that way.

24:21- Yeah, absolutely.

24:23And so the RISC era made things--

24:26- RISC?

24:27- So RISC chips, R-I-S-C.

24:29The RISC chips as opposed to CISC chips.

24:33The RISC chips made things more complicated

24:35for the compiler because what they ended up doing

24:39is ending up adding pipelines to the processor

24:42where the processor can do more than one thing at a time.

24:44But this means that the order of operations matters a lot.

24:47And so one of the classical compiler techniques

24:49that you use is called scheduling.

24:51And so moving the instructions around

24:54so that the processor can keep its pipelines full

24:57instead of stalling and getting blocked.

24:59And so there's a lot of things like that

25:00that are kind of bread and butter compiler techniques

25:03that have been studied a lot over the course of decades now.

25:06But the engineering side of making them real

25:08is also still quite hard.

25:10And you talk about machine learning.

25:12This is a huge opportunity for machine learning

25:14because many of these algorithms are full of these

25:17like hokey hand-rolled heuristics

25:19which work well on specific benchmarks

25:20but don't generalize and full of magic numbers.

25:23And I hear there's some techniques

25:26that are good at handling that.

25:28- So what would be the,

25:29if you were to apply machine learning to this,

25:33what's the thing you're trying to optimize?

25:34Is it ultimately the running time

25:38of a specific-- - Yeah, yeah.

25:39You can pick your metric and there's running time,

25:41there's memory use,

25:42there's lots of different things that you can optimize for.

25:45Code size is another one that some people care about

25:47in the embedded space.

25:48- Is this like the thinking into the future

25:51or is somebody actually been crazy enough to try

25:55to have machine learning-based parameter tuning

25:59for the optimization of compilers?

26:01- So this is something that is,

26:02I would say, research right now.

26:04There are a lot of research systems

26:06that have been applying search in various forms

26:09and using reinforcement learning as one form

26:11but also brute force search has been tried for quite a while.

26:14And usually these are in small problem spaces.

26:18So find the optimal way to code generate

26:21a matrix multiply for a GPU, right?

26:23Something like that where you say,

26:25there there's a lot of design space of,

26:28do you unroll loops a lot?

26:29Do you execute multiple things in parallel?

26:32And there's many different confounding factors here

26:35because graphics cards have different numbers

26:37of threads and registers and execution ports

26:40and memory bandwidth and many different constraints

26:42that interact in nonlinear ways.

26:44And so search is very powerful for that

26:46and it gets used in certain ways

26:49but it's not very structured.

26:51This is something that we as an industry need to fix.

26:54- So you said 80s but like,

26:56so have there been like big jumps

26:59in improvement and optimization?

27:01- Yeah.

27:02- Yeah, since then what's the coolest thing?

27:05- It's largely been driven by hardware.

27:07So hardware and software.

27:09So in the mid nineties, Java totally changed the world.

27:13Right, and I'm still amazed by how much change

27:17was introduced by Java.

27:17- In a good way or?

27:18- In a good way.

27:19So like reflecting back,

27:20Java introduced things like,

27:22all at once introduced things like JIT compilation.

27:25I mean, none of these were novel

27:26but it pulled it together and made it mainstream

27:28and made people invest in it.

27:30JIT compilation, garbage collection,

27:32portable code, safe code, like memory safe code,

27:36like a very dynamic dispatch execution model.

27:41Like many of these things,

27:42which had been done in research systems

27:44and had been done in small ways in various places,

27:46really came to the forefront

27:48and really changed how things worked

27:49and therefore changed the way people

27:51thought about the problem.

27:53JavaScript was another major world change

27:56based on the way it works.

27:57But also on the hardware side of things,

28:02multi-core and vector instructions

28:05really change the problem space

28:07and are very,

28:08they don't remove any of the problems

28:10that compilers faced in the past,

28:12but they add new kinds of problems

28:14of how do you find enough work

28:16to keep a four wide vector busy, right?

28:20Or if you're doing a matrix multiplication,

28:22how do you do different columns out of that matrix

28:25in at the same time

28:26and how do you maximally utilize

28:28the arithmetic compute that one core has?

28:31And then how do you take it to multiple cores?

28:33- How did the whole virtual machine thing change

28:35the compilation pipeline?

28:37- Yeah, so what the Java virtual machine does

28:40is it splits,

28:43just like I was talking about before,

28:44where you have a front end that parses the code

28:46and then you have an intermediate representation

28:47that gets transformed.

28:49What Java did was they said,

28:50we will parse the code

28:51and then compile to what's known as Java bytecode.

28:54And that bytecode is now a portable code representation

28:58that is industry standard and locked down

29:00and can't change.

29:02And then the back part of the compiler

29:05that does optimization and code generation

29:07can now be built by different vendors.

29:09And Java bytecode can be shipped around across the wire.

29:12It's memory safe and relatively trusted.

29:15- And because of that, it can run in the browser.

29:18- And that's why it runs in the browser, right?

29:20And so that way you can be in,

29:22again, back in the day,

29:23you would write a Java applet

29:24and you'd use as a web developer,

29:27you'd build this mini app that would run on a webpage.

29:30Well, a user of that is running a web browser

29:33on their computer.

29:34You download that Java bytecode,

29:36which can be trusted,

29:37and then you do all the compiler stuff on your machine

29:41so that you know that you trust that.

29:42- Now, is that a good idea or a bad idea?

29:44- It's a great idea.

29:44I mean, it's a great idea for certain problems.

29:46And I'm very much a believer

29:48that technology is itself neither good nor bad.

29:50It's how you apply it.

29:51You know, this would be a very, very bad thing

29:54for very low levels of the software stack,

29:56but in terms of solving

29:58some of these software portability and transparency

30:01or portability problems,

30:02I think it's been really good.

30:04Now, Java ultimately didn't win out on the desktop.

30:06And like, there are good reasons for that,

30:09but it's been very successful on servers

30:12and in many places,

30:13it's been a very successful thing over decades.

30:16- So what has been LLVMs

30:21and C-Langs improvements in optimization

30:26that throughout its history,

30:28what are some moments where you had set back

30:31and really proud of what's been accomplished?

30:33- Yeah, I think that the interesting thing about LLVM

30:36is not the innovations in compiler research.

30:40It has very good implementations

30:41of various important algorithms, no doubt.

30:43And a lot of really smart people have worked on it.

30:48But I think that the thing that's most profound about LLVM

30:50is that through standardization,

30:52it made things possible

30:53that otherwise wouldn't have happened, okay?

30:56And so interesting things that have happened with LLVM,

30:59for example, Sony has picked up LLVM

31:01and used it to do all the graphics compilation

31:03in their movie production pipeline.

31:06And so now they're able to have better special effects

31:07because of LLVM.

31:09That's kind of cool.

31:11That's not what it was designed for, right?

31:12But that's the sign of good infrastructure

31:15when it can be used in ways it was never designed for

31:18because it has good layering and software engineering

31:20and it's composable and things like that.

31:23- Which is where, as you said, it differs from GCC.

31:26- Yes, GCC is also great in various ways,

31:28but it's not as good as infrastructure technology.

31:31It's really a C compiler or it's a Fortran compiler.

31:36It's not infrastructure in the same way.

31:38- Now you can tell I don't know what I'm talking about

§05Transcript · 31:41–40:16

31:41because I keep saying C-lang.

31:43You can always tell when a person is close,

31:48by the way, pronounce something.

31:49I don't think, have I ever used clang?

31:52- Entirely possible.

31:53Have you, well, so you've used code,

31:55it's generated probably.

31:58So clang is an LLVM we use to compile

32:01all the apps on the iPhone effectively and the OS's.

32:05It compiles Google's production server applications.

32:09It's used to build GameCube games and PlayStation 4

32:14and things like that.

32:16- So as a user I have, but just everything I've done

32:20that I experienced with Linux has been,

32:22I believe, always GCC.

32:23- Yeah, I think Linux still defaults to GCC.

32:26- And is there a reason for that?

32:27Or is it because, I mean, is there a reason for that?

32:29- It's a combination of technical and social reasons.

32:32Many Linux developers do use clang,

32:35but the distributions for lots of reasons

32:39use GCC historically and they've not switched.

32:43- And just anecdotally online,

32:46it seems that LLVM has either reached the level GCC

32:50or superseded on different features or whatever.

32:53- The way I would say it is that they're so close

32:55it doesn't matter.

32:56- Yeah, exactly.

32:56- Like they're slightly better in some ways,

32:58slightly worse in other ways,

32:59but it doesn't actually really matter anymore at that level.

33:03- So in terms of optimization breakthroughs,

33:06it's just been solid incremental work.

33:09- Yeah, yeah, which describes a lot of compilers.

33:12The hard thing about compilers in my experience

33:14is the engineering, the software engineering,

33:17making it so that you can have hundreds of people

33:20collaborating on really detailed low-level work

33:23and scaling that, and that's really hard.

33:27And that's one of the things I think LLVM has done well.

33:30And that kind of goes back to the original design goals

33:34with it to be modular and things like that.

33:37And incidentally, I don't want to take all the credit

33:38for this, right?

33:39I mean, some of the best parts about LLVM

33:41is that it was designed to be modular.

33:43And when I started, I would write, for example,

33:45a register allocator, and then somebody much smarter than me

33:48would come in and pull it out and replace it

33:50with something else that they would come up with.

33:52And because it's modular, they were able to do that.

33:55And that's one of the challenges with GCC, for example,

33:58is replacing subsystems is incredibly difficult.

34:01It can be done, but it wasn't designed for that.

34:04And that's one of the reasons that LLVM's been

34:06very successful in the research world as well.

34:08- But in a community sense, Guido van Rossum, right,

34:12from Python, just retired from, what is it,

34:18Benevolent Dictator for Life, right?

34:20So in managing this community of brilliant compiler folks,

34:24did it, for a time at least, fall on you to approve things?

34:30- Oh yeah, so I mean, I still have something

34:34where I can order a magnitude more patches in LLVM

34:37than anybody else.

34:39And many of those I wrote myself.

34:42- But you still write, I mean, you're still close

34:46to the, I don't know what the expression is,

34:49to the metal, you still write code.

34:51- Yeah, I still write code.

34:52Not as much as I was able to in grad school,

34:54but that's an important part of my identity.

34:56But the way that LLVM has worked over time

34:58is that when I was a grad student, I could do all the work

35:01and steer everything and review every patch

35:04and make sure everything was done exactly

35:06the way my opinionated sense felt like it should be done.

35:10And that was fine.

35:11But at sync scale, you can't do that, right?

35:14And so what ends up happening is LLVM has a hierarchical

35:18system of what's called co-donors.

35:20These co-donors are given the responsibility

35:22not to do all the work,

35:24not necessarily to review all the patches,

35:26but to make sure that the patches do get reviewed

35:28and make sure that the right thing's happening

35:30architecturally in their area.

35:32And so what you'll see is you'll see that,

35:34for example, hardware manufacturers end up owning

35:38the hardware specific parts of their hardware.

35:43That's very common.

35:44Leaders in the community that have done really good work

35:47naturally become the de facto owner of something.

35:50And then usually somebody else is like,

35:53how about we make them the official co-donor?

35:55And then we'll have somebody to make sure

35:58that all the patches get reviewed in a timely manner.

36:00And then everybody's like, yes, that's obvious.

36:02And then it happens, right?

36:03And usually this is a very organic thing, which is great.

36:06And so I'm nominally the top of that stack still,

36:08but I don't spend a lot of time reviewing patches.

36:11What I do is I help negotiate a lot of the

36:16technical disagreements that end up happening

36:18and making sure that the community as a whole

36:19makes progress and is moving in the right direction

36:22and doing that.

36:23So we also started a nonprofit six years ago, seven years ago.

36:28Time's gone away.

36:30And the nonprofit, the LLVM Foundation nonprofit

36:34helps oversee all the business sides of things

36:35and make sure that the events that the LLVM community has

36:38are funded and set up and run correctly

36:41and stuff like that.

36:42But the foundation is very much,

36:44stays out of the technical side of

36:46where the project is going.

36:49- Right, so it sounds like a lot of it is just organic.

36:52Just...

36:53- Yeah, well, and this is, LLVM is almost 20 years old,

36:55which is hard to believe.

36:56Somebody pointed out to me recently that

36:58LLVM is now older than GCC was when LLVM started, right?

37:04So time has a way of getting away from you.

37:06But the good thing about that is it has a really robust,

37:10really amazing community of people that are

37:13in their professional lives,

37:14spread across lots of different companies,

37:16but it's a community of people that are interested

37:19in similar kinds of problems

37:21and have been working together effectively for years

37:23and have a lot of trust and respect for each other.

37:26And even if they don't always agree that,

37:28we're able to find a path forward.

37:31- So then in a slightly different flavor of effort,

37:34you started Apple in 2005 with the task of making,

37:38I guess, LLVM production ready.

37:41And then eventually 2013 through 2017,

37:44leading the entire developer tools department.

37:48We're talking about LLVM, Xcode, Objective-C to Swift.

37:52So in a quick overview of your time there,

37:58what were the challenges?

37:59First of all, leading such a huge group of developers,

38:03what was the big motivator dream mission

38:06behind creating Swift, the early birth of it

38:11from Objective-C and so on and Xcode?

38:13What are the challenges?

38:14- So these are different questions.

38:15- Yeah, I know, but I wanna talk about the other stuff too.

38:19- I'll stay on the technical side,

38:21then we can talk about the big team pieces, if that's okay.

38:24- So I used to really oversimplify many years of hard work.

38:27LLVM started, joined Apple, became a thing,

38:32became successful and became deployed.

38:34But then there's a question about

38:35how do we actually parse the source code?

38:38So LLVM is that back part,

38:40the optimizer and the code generator.

38:42And LLVM is really good for Apple

38:44as it went through a couple of hardware transitions.

38:46I joined right at the time of the Intel transition,

38:47for example, and 64-bit transitions,

38:51and then the transition to ARM with the iPhone.

38:53And so LLVM was very useful

38:54for some of these kinds of things.

38:56But at the same time, there's a lot of questions

38:58around developer experience.

39:00And so if you're a programmer pounding out

39:01at the time objective C code,

39:03the error message you get, the compile time,

39:06the turnaround cycle, the tooling and the IDE

39:09were not great, were not as good as they could be.

39:12And so, as I occasionally do, I'm like,

39:17well, okay, how hard is it to write a C compiler?

39:20And so I'm not gonna commit to anybody,

39:22I'm not gonna tell anybody,

39:23I'm just gonna just do it on nights and weekends

39:25and start working on it.

39:27And then I built up and see there's this thing

39:30called the preprocessor, which people don't like,

39:32but it's actually really hard and complicated

39:35and includes a bunch of really weird things

39:37like trigraphs and other stuff like that

39:39that are really nasty.

39:40And it's the crux of a bunch of the performance issues

39:44in the compiler.

39:45Started working on the parser

39:46and kind of got to the point where I'm like,

39:47oh, you know what, we could actually do this.

39:49Everybody's saying that this is impossible to do,

39:51but it's actually just hard, it's not impossible.

39:53And eventually told my manager about it,

39:57and he's like, oh, wow, this is great,

39:59we do need to solve this problem.

40:00Oh, this is great, we can get you one other person

40:02to work with you on this.

40:04And slowly a team is formed and it starts taking off.

40:08And C++, for example, huge complicated language,

40:12people always assume that it's impossible to implement,

40:14and it's very nearly impossible,

40:16but it's just really, really hard.

§06Transcript · 40:18–48:43

40:18And the way to get there is to build it

40:20one piece at a time, incrementally.

40:22And that was only possible because we were lucky

40:26to hire some really exceptional engineers

40:28that knew various parts of it very well

40:30and could do great things.

40:32Swift was kind of a similar thing.

40:34So Swift came from, we were just finishing off

40:39the first version of C++ support in Clang.

40:42And C++ is a very formidable and very important language,

40:47but it's also ugly in lots of ways.

40:49And you can't influence C++ without thinking

40:52there has to be a better thing, right?

40:54And so I started working on Swift, again,

40:56with no hope or ambition that would go anywhere,

40:58just let's see what could be done,

41:00let's play around with this thing.

41:02It was me in my spare time,

41:04not telling anybody about it kind of a thing.

41:08And it made some good progress.

41:09I'm like, actually, it would make sense to do this.

41:11At the same time, I started talking with the senior VP

41:14of software at the time, a guy named Bertrand Serlet.

41:17And Bertrand was very encouraging.

41:19He was like, well, let's have fun, let's talk about this.

41:22And he was a little bit of a language guy.

41:23And so he helped guide some of the early work

41:26and encouraged me and got things off the ground.

41:30And eventually, I told my manager and told other people,

41:34and it started making progress.

41:38The complicating thing with Swift was that

41:41the idea of doing a new language is not obvious to anybody,

41:45including myself.

41:46And the tone at the time was that the iPhone was successful

41:50because of Objective-C, right?

41:53- Oh, interesting.

41:54- In Objective-C. - Not despite of,

41:55or just because of. - Correct.

41:57And you have to understand that at the time,

41:59Apple was hiring software people that loved Objective-C.

42:05Right, and it wasn't that they came despite Objective-C.

42:07They loved Objective-C, and that's why they got hired.

42:10And so you had a software team that the leadership

42:12in many cases went all the way back to Next,

42:15where Objective-C really became real.

42:19And so they quote unquote grew up writing Objective-C.

42:23And many of the individual engineers all were hired

42:26because they loved Objective-C.

42:28And so this notion of, okay, let's do new language

42:30was kind of heretical in many ways, right?

42:34Meanwhile, my sense was that the outside community

42:36wasn't really in love with Objective-C.

42:37Some people were, and some of the most outspoken people were,

42:40but other people were hitting challenges

42:42because it has very sharp corners and it's difficult to learn.

42:46And so one of the challenges of making Swift happen

42:50that was totally non-technical is the social part

42:54of what do we do?

42:57Like if we do a new language, which at Apple,

42:59many things happen that don't ship, right?

43:02So if we ship it, what is the metrics of success?

43:05Why would we do this?

43:06Why wouldn't we make Objective-C better?

43:07If Objective-C has problems,

43:09let's file off those rough corners and edges.

43:12And one of the major things that became the reason

43:15to do this was this notion of safety, memory safety.

43:18And the way Objective-C works is that a lot

43:22of the object system and everything else

43:24is built on top of pointers in C.

43:27Objective-C is an extension on top of C.

43:29And so pointers are unsafe.

43:32And if you get rid of the pointers,

43:34it's not Objective-C anymore.

43:36And so fundamentally, that was an issue

43:39that you could not fix safety or memory safety

43:42without fundamentally changing the language.

43:45And so once we got through that part

43:47of the mental process and the thought process,

43:50it became a design process of saying,

43:53okay, well, if we're gonna do something new, what is good?

43:56Like, how do we think about this?

43:57And what do we like?

43:58And what are we looking for?

43:59And that was a very different phase of it.

44:02- So what are some design choices early on in Swift?

44:05Like we're talking about braces.

44:08Are you making a type language or not?

44:12All those kinds of things.

44:13- Yeah, so some of those were obvious given the context.

44:16So a typed language, for example,

44:17Objective-C is a typed language.

44:19And going with an untyped language

44:20wasn't really seriously considered.

44:24We wanted the performance and we wanted refactoring tools

44:27and other things like that to go with typed languages.

44:29- Quick dumb question.

44:31Was it obvious, I think this would be a dumb question,

44:34but was it obvious that the language

44:36has to be a compiled language?

44:38- Yes, that's not a dumb question.

44:42Earlier, I think late '90s,

44:43Apple had seriously considered moving

44:45its development experience to Java.

44:47But Swift started in 2010,

44:51which was several years after the iPhone.

44:53It was when the iPhone was definitely on an upper trajectory

44:56and the iPhone was still extremely,

44:58and is still a bit memory constrained, right?

45:01And so being able to compile the code and then ship it

45:05and then having standalone code that is not JIT compiled

45:09is a very big deal and is very much part

45:12of the Apple value system.

45:15Now, JavaScript's also a thing, right?

45:17I mean, it's not that this is exclusive

45:19and technologies are good

45:21depending on how they're applied, right?

45:23But in the design of Swift,

45:26saying like, how can we make Objective-C better, right?

45:28Objective-C is statically compiled

45:29and that was the contiguous, natural thing to do.

45:32- Just skip ahead a little bit.

45:34Right back, just as a question,

45:36as you think about today in 2019,

45:40in your work at Google, TensorFlow and so on,

45:42is, again, compilations, static compilation,

45:46still the right thing?

45:49- Yeah, so the funny thing after working on compilers

45:52for a really long time is that,

45:54and this is one of the things that LLVM has helped with,

45:58is that I don't look at compilations being static

46:02or dynamic or interpreted or not.

46:05This is a spectrum.

46:07And one of the cool things about Swift

46:09is that Swift is not just statically compiled.

46:12It's actually dynamically compiled as well.

46:14And it can also be interpreted,

46:15though nobody's actually done that.

46:17And so what ends up happening

46:20when you use Swift in a workbook,

46:22for example, in Colab or in Jupyter,

46:24is it's actually dynamically compiling the statements

46:26as you execute them.

46:28And so this gets back to the software engineering problems,

46:32right, where if you layer the stack properly,

46:34you can actually completely change

46:37how and when things get compiled

46:38because you have the right abstractions there.

46:41And so the way that a Colab workbook works with Swift

46:44is that when you start typing into it,

46:47it creates a process, a Unix process,

46:50and then each line of code you type in,

46:52it compiles it through the Swift compiler,

46:54the front-end part,

46:56and then sends it through the optimizer,

46:58JIT compiles machine code,

47:00and then injects it into that process.

47:03And so as you're typing new stuff,

47:05it's like squirting in new code

47:08and overwriting and replacing and updating code in place.

47:11And the fact that it can do this is not an accident.

47:13Like Swift was designed for this,

47:15but it's an important part of how the language was set up

47:18and how it's layered, and this is a non-obvious piece.

47:21And one of the things with Swift that was,

47:24for me, a very strong design point

47:25is to make it so that you can learn it very quickly.

47:29And so from a language design perspective,

47:32the thing that I always come back to is this UI principle

47:34of progressive disclosure of complexity.

47:37And so in Swift, you can start by saying print,

47:41quote, hello world, quote, right?

47:43And there's no slash N, just like Python,

47:46one line of code, no main, no--

47:48- No header files.

47:49- No header files, no public static class void,

47:51blah, blah, blah, string, like Java has, right?

47:54So one line of code, right?

47:55And you can teach that and it works great.

47:58Then you can say, well, let's introduce variables.

48:00And so you can declare a variable with var,

48:02so var x equals four, what is a variable?

48:04You can use x, x plus one, this is what it means.

48:07Then you can say, well, how about control flow?

48:09Well, this is what an if statement is.

48:10This is what a for statement is.

48:12This is what a while statement is.

48:13Then you can say, let's introduce functions, right?

48:17And many languages like Python have had this kind of notion

48:21of let's introduce small things

48:22and then you can add complexity,

48:24then you can introduce classes,

48:25and then you can add generics in the case of Swift,

48:28and then you can, in modules,

48:29then build out in terms of the things that you're expressing.

48:32But this is not very typical for compiled languages.

48:35And so this was a very strong design point

48:38and one of the reasons that Swift in general

48:40is designed with this factoring of complexity in mind

48:43so that the language can express powerful things.

§07Transcript · 48:46–57:02

48:46You can write firmware in Swift if you want to,

48:49but it has a very high level feel,

48:51which is really this perfect blend

48:53because often you have very advanced library writers

48:57that want to be able to use the nitty gritty details,

49:00but then other people just want to use the libraries

49:02and work at a higher abstraction level.

49:04- It's kind of cool that I saw

49:06that you can just interoperability.

49:09I don't think I pronounced that word enough,

49:11but you can just drag in Python.

49:13It's just straight, you can import,

49:16like I saw this in the demo, import and import.

49:19How do you make that happen?

49:20- Yeah, well.

49:21- What's up with, is that as easy as it looks or is it?

49:25- Yes, as easy as it looks.

49:26That's not a stage magic hack or anything like that.

49:29- No, no, I don't mean from the user perspective,

49:31I mean from the implementation perspective,

49:33to make it happen.

49:34- So it's easy once all the pieces are in place.

49:36The way it works, so if you think about

49:38a dynamically typed language like Python,

49:40you can think about it in two different ways.

49:42You can say it has no types,

49:44which is what most people would say,

49:47or you can say it has one type,

49:49and you can say it has one type

49:51and it's like the Python object.

49:53The Python object gets passed around

49:55and because there's only one type, it's implicit.

49:57And so what happens with Swift

50:00and Python talking to each other,

50:01Swift has lots of types, it has arrays and it has strings

50:04and all like classes and that kind of stuff,

50:07but it now has a Python object type.

50:11So there is one Python object type.

50:12And so when you say import NumPy,

50:15what you get is a Python object, which is the NumPy module.

50:19And then you say np.array, and it says,

50:22okay, hey, Python object, I have no idea what you are,

50:24give me your array member.

50:26Okay, cool.

50:28And it just uses dynamic stuff,

50:30talks to the Python interpreter and says,

50:31hey, Python, what's the .array member in that Python object?

50:35It gives you back another Python object.

50:37And now you say parentheses for the call

50:39and the arguments you're gonna pass.

50:40And so then it says, hey, a Python object

50:43that is the result of np.array, call with these arguments.

50:48Again, calling into the Python interpreter to do that work.

50:50And so right now, this is all really simple.

50:53And if you dive into the code,

50:55what you'll see is that the Python module in Swift

50:58is something like 1200 lines of code or something.

51:01It's written in pure Swift.

51:02It's super simple.

51:03And it's built on top of the C interoperability

51:06because it just talks to the Python interpreter.

51:09But making that possible required us

51:11to add two major language features to Swift

51:13to be able to express these dynamic calls

51:15and the dynamic member lookups.

51:17And so what we've done over the last year

51:19is we've proposed, implement, standardized

51:23and contributed new language features to the Swift language

51:26in order to make it so it is really trivial.

51:28Right, and this is one of the things about Swift

51:31that is critical to the Swift for TensorFlow work,

51:34which is that we can actually add new language features.

51:37And the bar for adding those is high,

51:39but it's what makes it possible.

51:42- So you're now at Google doing incredible work

51:45on several things, including TensorFlow.

51:47So TensorFlow 2.0 or whatever leading up to 2.0

51:52has by default in 2.0 has eager execution.

51:56And yet in order to make code optimized for GPU or TPU

52:00or some of these systems,

52:03computation needs to be converted to a graph.

52:05So what's that process like?

52:07What are the challenges there?

52:08- Yeah, so I'm tangentially involved in this,

52:11but the way that it works with autograph

52:15is that you mark your function with a decorator.

52:21And when Python calls it, that decorator is invoked.

52:24And then it says, before I call this function,

52:28you can transform it.

52:29And so the way autograph works is,

52:31as far as I understand is it actually uses the Python parser

52:34to go parse that, turn it into a syntax tree,

52:37and now apply compiler techniques to, again,

52:39transform this down into TensorFlow graphs.

52:42And so you can think of it as saying,

52:44hey, I have an if statement,

52:45I'm gonna create an if node in the graph,

52:48like you say, tf.cond.

52:51You have a multiply,

52:53well, I'll turn that into a multiply node in the graph,

52:55and it becomes this tree transformation.

52:57- So where does the Swift for TensorFlow come in?

53:01Which is, you know, parallels.

53:04You know, for one, Swift is a interface,

53:06like Python is an interface to TensorFlow,

53:09but it seems like there's a lot more going on

53:11in just a different language interface.

53:13There's optimization methodology.

53:14- Yeah, so the TensorFlow world has a couple of different,

53:19what I'd call front-end technologies.

53:21And so Swift and Python and Go and Rust and Julia

53:25and all these things share the TensorFlow graphs

53:29and all the runtime and everything that's later.

53:32And so Swift for TensorFlow is merely another front-end

53:36for TensorFlow, just like any of these other systems are.

53:40There's a major difference between,

53:42I would say, three camps of technologies here.

53:44There's Python, which is a special case,

53:46because the vast majority of the community effort

53:49is going to the Python interface.

53:51And Python has its own approaches

53:52for automatic differentiation,

53:54it has its own APIs and all this kind of stuff.

53:57There's Swift, which I'll talk about in a second.

54:00And then there's kind of everything else.

54:01And so the everything else are effectively language bindings.

54:05So they call into the TensorFlow runtime,

54:08but they're not,

54:08they usually don't have automatic differentiation

54:10or they usually don't provide anything other than APIs

54:14that call the C APIs in TensorFlow.

54:16And so they're kind of wrappers for that.

54:18Swift is really kind of special.

54:19And it's a very different approach.

54:21Swift for TensorFlow, that is,

54:24is a very different approach,

54:25because there we're saying,

54:26let's look at all the problems that need to be solved

54:29in the full stack of the TensorFlow compilation process,

54:34if you think about it that way.

54:35Because TensorFlow is fundamentally a compiler.

54:38It takes models, and then it makes them go fast on hardware.

54:42That's what a compiler does.

54:43And it has a front-end, it has an optimizer,

54:47and it has many back-ends.

54:49And so if you think about it the right way,

54:51or if you look at it in a particular way,

54:54like it is a compiler.

54:56And so Swift is merely another front-end.

55:02But it's saying, and the design principle is saying,

55:05let's look at all the problems that we face

55:07as machine learning practitioners,

55:09and what is the best possible way we can do that,

55:12given the fact that we can change

55:13literally anything in this entire stack.

55:15And Python, for example, where the vast majority

55:18of the engineering and effort has gone into,

55:22is constrained by being the best possible thing

55:24you can do with a Python library.

55:27Like there are no Python language features

55:29that are added because of machine learning

55:31that I'm aware of.

55:32They added a matrix multiplication operator with that,

55:35but that's as close as you get.

55:37And so with Swift, it's hard,

55:41but you can add language features to the language,

55:43and there's a community process for that.

55:45And so we look at these things and say,

55:48well, what is the right division of labor

55:49between the human programmer and the compiler?

55:51And Swift has a number of things that shift that balance.

55:55So because it has a type system, for example,

56:00it makes certain things possible for analysis of the code,

56:03and the compiler can automatically build graphs for you

56:06without you thinking about them.

56:08Like that's a big deal for a programmer.

56:10You just get free performance,

56:11you get clustering and fusion and optimization,

56:14things like that,

56:16without you as a programmer having to manually do it

56:18because the compiler can do it for you.

56:20Automatic differentiation is another big deal.

56:22And I think one of the key contributions

56:24of the Swift for TensorFlow project

56:27is that there's this entire body of work

56:30on automatic differentiation

56:32that dates back to the Fortran days.

56:34People doing a tremendous amount of numerical computing

56:36in Fortran used to write these,

56:38what they call source to source translators,

56:40where you take a bunch of code,

56:43shove it into a mini compiler,

56:45and it would push out more Fortran code,

56:48but it would generate the backwards passes

56:50for your functions for you, the derivatives.

56:52And so in that work in the '70s,

56:57a tremendous number of optimizations,

56:58a tremendous number of techniques

57:01for fixing numerical instability

57:02and other kinds of problems were developed,

§08Transcript · 57:05–1:04:57

57:05but they're very difficult to port into a world

57:07where in eager execution,

57:09you get an op by op at a time.

57:11Like you need to be able to look at an entire function

57:13and be able to reason about what's going on.

57:15And so when you have a language integrated

57:18automatic differentiation,

57:19which is one of the things

57:20that the Swift project is focusing on,

57:22you can open all these techniques

57:24and reuse them in familiar ways.

57:28But the language integration piece

57:30has a bunch of design room in it,

57:31and it's also complicated.

57:33- The other piece of the puzzle here

57:34that's kind of interesting is TPUs at Google.

57:37- Yes.

57:37- So we're in a new world with deep learning.

57:40It constantly is changing,

57:41and I imagine, without disclosing anything,

57:45I imagine you're still innovating on the TPU front too.

57:48- Indeed.

57:49- So how much sort of interplay is there

57:52between software and hardware

57:53in trying to figure out how to together

57:55move towards an optimized solution?

57:56- There's an incredible amount.

57:57So we're on our third generation of TPUs,

57:59which are now 100 petaflops

58:01in a very large liquid-cooled box,

58:04a virtual box with no cover.

58:07And as you might imagine, we're not out of ideas yet.

58:11The great thing about TPUs

58:13is that they're a perfect example

58:15of hardware-software co-design.

58:17And so it's about saying,

58:19what hardware do we build to solve

58:20certain classes of machine learning problems?

58:23Well, the algorithms are changing.

58:26Like the hardware takes, you know,

58:28some cases years to produce, right?

58:30And so you have to make bets

58:31and decide what is going to happen.

58:34And so, and what is the best way

58:36to spend the transistors

58:37to get the maximum, you know,

58:38performance per watt or area per cost,

58:41or like whatever it is that you're optimizing for.

58:43And so one of the amazing things about TPUs

58:46is this numeric format called BFloat16.

58:50BFloat16 is a compressed 16-bit floating point format,

58:54but it puts the bits in different places.

58:56And in numeric terms,

58:57it has a smaller mantissa and a larger exponent.

59:00That means that it's less precise,

59:02but it can represent larger ranges of values,

59:05which in the machine learning context

59:07is really important and useful

59:08because sometimes you have very small gradients

59:13you want to accumulate

59:13and very, very small numbers

59:16that are important to move things as you're learning,

59:20but sometimes you have very large magnitude numbers as well.

59:23And BFloat16 is not as precise, the mantissa is small,

59:28but it turns out the machine learning algorithms

59:30actually want to generalize.

59:31And so there's, you know,

59:33theories that this actually increases

59:35the ability for the network to generalize across datasets.

59:38And regardless of whether it's good or bad,

59:41it's much cheaper at the hardware level to implement

59:43because the area and time of a multiplier

59:48is N squared in the number of bits in the mantissa,

59:50but it's linear with size of the exponent.

59:53- And you're connected to both efforts here,

59:55both on the hardware and the software side?

59:57- Yeah, and so that was a breakthrough

59:58coming from the research side

60:01and people working on optimizing network transport

60:04of weights across the network originally

60:07and trying to find ways to compress that.

60:10But then it got burned into silicon

60:12and it's a key part of what makes TPU performance so amazing

60:15and great.

60:17Now TPUs have many different aspects of it

60:19that are important,

60:20but the co-design between the low-level compiler bits

60:25and the software bits and the algorithms

60:27is all super important.

60:28And it's this amazing trifecta that only Google can do.

60:32- Yeah, that's super exciting.

60:34So can you tell me about MLIR project,

60:38previously the secretive one?

60:41- Yeah, so MLIR is a project that we announced

60:43at a compiler conference three weeks ago or something,

60:47the Compilers for Machine Learning Conference.

60:50Basically, again, if you look at TensorFlow

60:52as a compiler stack,

60:53it has a number of compiler algorithms within it.

60:56It also has a number of compilers that get embedded into it

60:59and they're made by different vendors.

61:00For example, Google has XLA,

61:02which is a great compiler system.

61:04NVIDIA has TensorRT, Intel has Ngraph.

61:08There's a number of these different compiler systems

61:10and they're very hardware specific

61:13and they're trying to solve different parts of the problems,

61:16but they're all kind of similar in a sense

61:19of they wanna integrate with TensorFlow.

61:20Now TensorFlow has an optimizer

61:22and it has these different code generation technologies

61:25built in.

61:26The idea of MLIR is to build a common infrastructure

61:28to support all these different subsystems.

61:31And initially it's to be able to make it

61:33so that they all plug in together

61:34and they can share a lot more code and can be reusable.

61:37But over time,

61:38we hope that the industry will start collaborating

61:41and sharing code.

61:42And instead of reinventing the same things

61:43over and over again,

61:45that we can actually foster some of that,

61:47working together to solve common problem energy

61:51that has been useful in the compiler field before.

61:54Beyond that, MLIR is,

61:56some people have joked that it's kind of LLVM2.

61:59It learns a lot about what LLVM has been good

62:01and what LLVM has done wrong.

62:04And it's a chance to fix that.

62:06And also there are challenges in the LLVM ecosystem as well,

62:09where LLVM is very good at the thing it was designed to do,

62:12but 20 years later, the world has changed

62:15and people are trying to solve higher level problems

62:17and we need some new technology.

62:20- And what's the future of open source in this context?

62:24- Very soon.

62:25So it is not yet open source,

62:27but it will be hopefully in the next couple of months.

62:29- So you still believe in the value of open source

62:31in these kinds of contexts?

62:31- Oh yeah, absolutely.

62:32And I think that the TensorFlow community at large

62:36fully believes in open source.

62:37- So I mean, there is a difference between Apple,

62:40where you were previously, and Google now,

62:42in spirit and culture.

62:43And I would say the open sourcing of TensorFlow

62:45was a seminal moment in the history of software

62:48'cause here's this large company

62:51releasing a very large code base that's open sourcing.

62:55What are your thoughts on that?

62:57How happy or not were you to see

63:00that kind of degree of open sourcing?

63:02- So between the two, I prefer the Google approach,

63:05if that's what you're saying.

63:06The Apple approach makes sense

63:11given the historical context that Apple came from,

63:13but that's been 35 years ago.

63:15And I think that Apple is definitely adapting.

63:18And the way I look at it is that there's different kinds

63:21of concerns in this space, right?

63:23It is very rational for a business

63:24to care about making money.

63:28That fundamentally is what a business is about, right?

63:31But I think it's also incredibly realistic to say

63:34it's not your string library

63:36that's the thing that's gonna make you money.

63:38It's gonna be the amazing UI product differentiating features

63:42and other things like that

63:42that you build on top of your string library.

63:45And so keeping your string library proprietary and secret

63:49and things like that,

63:51maybe not the important thing anymore, right?

63:54Where before platforms were different, right?

63:57And even 15 years ago, things were a little bit different,

64:01but the world is changing.

64:02So Google strikes a very good balance, I think.

64:05And I think the TensorFlow being open source

64:08really changed the entire machine learning field

64:11and it caused a revolution in its own right.

64:14And so I think it's amazingly forward-looking

64:17because I could have imagined,

64:20and I wasn't at Google at the time,

64:21but I could imagine a different context

64:23and a different world where a company says,

64:25"Machine learning is critical to what we're doing.

64:27"We're not gonna give it to other people," right?

64:29And so that decision is a profoundly brilliant insight

64:34that I think has really led to the world being better

64:38and better for Google as well.

64:40- And has all kinds of ripple effects.

64:42I think it is really, I mean,

64:45you can't understate Google deciding that,

64:47how profound that is for software.

64:49It's awesome.

64:50- Well, and it's been, and again,

64:52I can understand the concern about

64:55if we release our machine learning software,

64:57our competitors could go faster.

§09Transcript · 1:05:00–1:12:58

65:00But on the other hand, I think that open sourcing TensorFlow

65:02has been fantastic for Google.

65:03And I'm sure that decision was very non-obvious at the time,

65:08but I think it's worked out very well.

65:11- So let's try this real quick.

65:13You were at Tesla for five months

65:15as the VP of Autopilot Software.

65:17You led the team during the transition

65:19from H hardware one to hardware two.

65:22I have a couple of questions.

65:23So one, first of all, to me,

65:26that's one of the bravest engineering decisions

65:28undertaking, sort of like,

65:31undertaking really ever in the automotive industry,

65:34to me, software-wise, starting from scratch.

65:37It's a really brave engineering decision.

65:39So my one question there is, what was that like?

65:42What was the challenge of that?

65:43- Do you mean the career decision of jumping

65:45from a comfortable good job into the unknown, or?

65:48- That combined, so at the individual level,

65:51you making that decision, and then when you show up,

65:56you know, it's a really hard engineering problem.

65:58So you could just stay, maybe slow down,

66:03say hardware one, or those kinds of decisions.

66:06Just taking it full on, let's do this from scratch.

66:10What was that like?

66:11- Well, so, I mean, I don't think Tesla has a culture

66:13of taking things slow and seeing how it goes.

66:15So, and one of the things that attracts me about Tesla

66:18is it's very much a gung-ho, let's change the world,

66:20let's figure it out kind of a place.

66:21And so I have a huge amount of respect for that.

66:24Tesla has done very smart things

66:27with hardware one in particular,

66:29and the hardware one design was originally designed

66:32to be very simple automation features in the car

66:37for like traffic-aware cruise control and things like that.

66:39And the fact that they were able to effectively

66:42feature creep it into lane holding

66:44and a very useful driver assistance feature

66:48is pretty astounding,

66:49particularly given the details of the hardware.

66:51Hardware two built on that in a lot of ways.

66:54And the challenge there was that they were transitioning

66:56from a third-party provided vision stack

67:00to an in-house built vision stack.

67:01And so for the first step, which I mostly helped with,

67:05was getting onto that new vision stack.

67:08And that was very challenging.

67:10And it was time critical for various reasons,

67:14and it was a big leap,

67:15but it was fortunate that it built on a lot of the knowledge

67:17and expertise in the team that had built

67:19hardware one's driver assistance features.

67:22- So you spoke in a collected and kind way

67:25about your time at Tesla,

67:26but it was ultimately not a good fit.

67:28Elon Musk, we've talked on this podcast,

67:31several guests of the course,

67:33Elon Musk continues to do some of the most bold

67:35and innovative engineering work in the world.

67:38At times at the cost to some of the members

67:40of the Tesla team,

67:41what did you learn about this working

67:44in this chaotic world with Elon?

67:45- Yeah, so I guess I would say that when I was at Tesla,

67:50I experienced and saw the highest degree of turnover

67:54I'd ever seen in a company, which was a bit of a shock.

67:57But one of the things I learned and I came to respect

68:00is that Elon's able to attract amazing talent

68:03because he has a very clear vision of the future

68:05and he can get people to buy into it

68:07because they want that future to happen.

68:09And the power of vision is something

68:11that I have a tremendous amount of respect for.

68:14And I think that Elon is fairly singular in the world

68:17in terms of the things he's able to get people to believe in.

68:22And it's a very,

68:24there may be people that stand in the street corner

68:27and say, "Ah, we're gonna go to Mars."

68:29But then there are a few people that can get others

68:33to buy into it and believe in,

68:34build the path and make it happen.

68:36And so I respect that.

68:38I don't respect all of his methods,

68:41but I have a huge amount of respect for that.

68:43- You've mentioned in a few places,

68:46including in this context, working hard.

68:50What does it mean to work hard?

68:51And when you look back at your life,

68:53what were some of the most brutal periods

68:57of having to really sort of put everything you have

69:01into something?

69:02- Yeah, good question.

69:05So working hard can be defined a lot of different ways.

69:07So a lot of hours.

69:08And so that is true.

69:11The thing to me that's the hardest

69:14is both being short-term focused on delivering

69:18and executing and making a thing happen,

69:20while also thinking about the longer term

69:23and trying to balance that, right?

69:24Because if you are myopically focused on solving a task

69:28and getting that done,

69:29and only think about that incremental next step,

69:32you will miss the next big hill you should jump over to.

69:36And so I've been really fortunate

69:38that I've been able to kind of oscillate between the two.

69:42And historically at Apple, for example,

69:45that was made possible because I was able to work

69:47with some really amazing people and build up teams

69:49and leadership structures

69:51and allow them to grow in their careers

69:55and take on responsibility,

69:57thereby freeing up me to be a little bit crazy

70:00and thinking about the next thing.

70:02And so it's a lot of that,

70:04but it's also about,

70:06with experience you make connections

70:08that other people don't necessarily make.

70:10And so I think that's a big part as well.

70:13But the bedrock is just a lot of hours

70:16and that's okay with me.

70:19There's different theories on work-life balance

70:21and my theory for myself,

70:23which I do not project onto the team,

70:25but my theory for myself is that,

70:26I wanna love what I'm doing and work really hard.

70:30And my purpose, I feel like,

70:33and my goal is to change the world

70:35and make it a better place.

70:36And that's what I'm really motivated to do.

70:38- So last question, LLVM logo is a dragon.

70:44- Yeah.

70:44- You explain that this is because dragons

70:46have connotations of power, speed, intelligence.

70:50It can also be sleek, elegant, and modular,

70:53though you remove the modular part.

70:55What is your favorite dragon-related character

70:58from fiction, video, or movies?

71:01- So those are all very kind ways of explaining it.

71:03Do you wanna know the real reason it's a dragon?

71:05- Well, yeah.

71:07- Is that better?

71:07So there's a seminal book on compiler design

71:11called "The Dragon Book."

71:12And so this is a really old now book on compilers.

71:16And so the dragon logo for LLVM came about

71:20because at Apple,

71:22we kept talking about LLVM-related technologies

71:24and there's no logo to put on a slide.

71:27And so we're like, "What do we do?"

71:28And somebody's like, "Well, what kind of logo

71:29"should a compiler technology have?"

71:32And I'm like, "I don't know.

71:33"I mean, the dragon is the best thing that we've got."

71:37And Apple somehow magically came up with the logo

71:40and it was a great thing

71:42and the whole community rallied around it.

71:44And then it got better

71:45as other graphic designers got involved.

71:47But that's originally where it came from.

71:49- Story.

71:50Is there dragons from fiction that you connect with?

71:52That "Game of Thrones," "Lord of the Rings,"

71:57that kind of thing?

71:58- "Lord of the Rings" is great.

71:59I also like role-playing games

72:00and things like computer role-playing games.

72:02And so dragons often show up in there.

72:03But it really comes back to the book.

72:07Oh no, we need a thing.

72:08- Yeah.

72:09- And hilariously, one of the funny things about LLVM

72:13is that my wife, who's amazing,

72:16runs the LLVM Foundation.

72:19And she goes to Grace Hopper

72:20and is trying to get more women involved in the,

72:23she's also a compiler engineer.

72:24So she's trying to get other women

72:26to get interested in compilers and things like this.

72:28And so she hands out the stickers.

72:29And people like the LLVM sticker

72:32because of "Game of Thrones."

72:34And so sometimes culture has this helpful effect

72:36to get the next generation of compiler engineers

72:39engaged with the cause.

72:42- Okay, awesome.

72:43Chris, thanks so much for talking to me.

72:44- Yeah, it's been great talking with you.

72:45(upbeat music)

72:48(upbeat music)

72:51(upbeat music)

72:53(upbeat music)

72:56(upbeat music)

72:58(upbeat music)

— 读到这里,多懂一点。 —