Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Media Programming Hardware

FFmpeg Devs Boast of Up To 94x Performance Boost After Implementing Handwritten AVX-512 Assembly Code (tomshardware.com) 135

Anton Shilov reports via Tom's Hardware: FFmpeg is an open-source video decoding project developed by volunteers who contribute to its codebase, fix bugs, and add new features. The project is led by a small group of core developers and maintainers who oversee its direction and ensure that contributions meet certain standards. They coordinate the project's development and release cycles, merging contributions from other developers. This group of developers tried to implement a handwritten AVX512 assembly code path, something that has rarely been done before, at least not in the video industry.

The developers have created an optimized code path using the AVX-512 instruction set to accelerate specific functions within the FFmpeg multimedia processing library. By leveraging AVX-512, they were able to achieve significant performance improvements -- from three to 94 times faster -- compared to standard implementations. AVX-512 enables processing large chunks of data in parallel using 512-bit registers, which can handle up to 16 single-precision FLOPS or 8 double-precision FLOPS in one operation. This optimization is ideal for compute-heavy tasks in general, but in the case of video and image processing in particular.

The benchmarking results show that the new handwritten AVX-512 code path performs considerably faster than other implementations, including baseline C code and lower SIMD instruction sets like AVX2 and SSSE3. In some cases, the revamped AVX-512 codepath achieves a speedup of nearly 94 times over the baseline, highlighting the efficiency of hand-optimized assembly code for AVX-512.

This discussion has been archived. No new comments can be posted.

FFmpeg Devs Boast of Up To 94x Performance Boost After Implementing Handwritten AVX-512 Assembly Code

Comments Filter:
  • Neat... But, (Score:5, Insightful)

    by Valgrus Thunderaxe ( 8769977 ) on Monday November 04, 2024 @05:55PM (#64919749)
    With a 94x improvement, someone needs to fix their compiler.
    • They're too busy, the compiler writers are working on breaking compatibility with doubtful optimizations. [cr.yp.to]
      • I mean the problem of unwanted optimization was kinda already solved in Pascal. There you pass compiler flags via special comments in your source code.

        Why not have something like that in C where you can, for example, override the -O option?

    • Re:Neat... But, (Score:5, Informative)

      by AmiMoJo ( 196126 ) on Tuesday November 05, 2024 @05:29AM (#64920691) Homepage Journal

      It's quite difficult to optimize this sort of stuff at compiler level because the compiler has to infer what the intent is, while still obeying all the rules that define the behaviour of C code.

      As a compiler developer you could expend a lot of effort optimizing for this particular scenario, but it wouldn't help many other projects. It's a case where assembler is a good option to get some application specific performance enhancements.

      I encountered a similar thing with ffmpeg on ARM some years ago. We needed to do some FFT calculations but the Microsoft compiler with it's intrinsic support was not producing fast enough output, so we used the code from ffmpeg which had hand written assembly for ARM NEON. Performance was orders of magnitude better and let us do it in real-time. Code was of course published as per GPL requirements.

  • I hope it's a real world benchmark not some contrived situation and propaganda from Intel.

    • by test321 ( 8891681 ) on Monday November 04, 2024 @06:28PM (#64919821)

      propaganda from Intel.

      If anything, the propaganda would be from AMD.

      Intel disabled AVX-512 for its Core 12th, 13th, and 14th Generations of Core processors, leaving owners of these CPUs without them. On the other hand, AMD's Ryzen 9000-series CPUs feature a fully-enabled AVX-512 FPU so the owners of these processors can take advantage of the FFmpeg achievement.

      • Yes, AVX-512 is just for professionals, didn't you know that?

        Buy our Xeon Scalable processors for 4-10x the price if you need your PC to do things like... decode video quickly and efficiently!

        No, don't look over at that other company. Look at me. You need professional. quality. chips.

    • It does seem pretty contrived, they've achieved a significant speedup on a graphics processing algorithm when run on a general-purpose CPU. How does that compare with, oh, I dunno, running the same graphics processing algorithm on a graphics processing unit? It's sort of like saying you're the fastest person around the Nurburgring in a van.
  • by dfghjk ( 711126 ) on Monday November 04, 2024 @06:07PM (#64919771)

    Why didn't they just ask ChatGPT to rewrite it? Handwritten? Haven't we been told there's no reason for that?

  • by ip_freely_2000 ( 577249 ) on Monday November 04, 2024 @06:09PM (#64919777)
    I've had 90%+ optimizations on certain data processing functions by hand coding and tuning instead of depending on libraries and other 'productivity' tools. In my coding life (which is long but fortunately very nearly over) we've added layer upon layer of complexity which is sometimes not necessary.
    • by backslashdot ( 95548 ) on Monday November 04, 2024 @06:18PM (#64919791)

      I stopped coding/optimizing in assembly over 20 years ago, and then only utilized knowledge of it for debugging, cybersecurity, or fun purposes for a few years. Nowadays I have zero use for it other than getting super annoyed that people don't know it.

      • by dsgrntlxmply ( 610492 ) on Monday November 04, 2024 @07:37PM (#64919985)

        Hey punk, get off my ROM!

        My last significant use of assembly was late 80s / early 90s. Working on embedded systems with 8-bit microprocessors, tiny boot ROM capacity, and rudimentary or no compiler, there was no choice. Around 2017, I had to take a deep dive into GCC compiled ARM code to characterize an obscure but dramatic failure in a specific embedded situation. This turned out to be incorrect code generated by GCC. Once characterized, it was not difficult to work around, but it required machine level knowledge to locate.

    • by Tony Isaac ( 1301187 ) on Monday November 04, 2024 @06:21PM (#64919801) Homepage

      That, and programmers often use boneheaded algorithms because they don't know any better.

      Remember Bubble Sort? If you tried to build the most inefficient algorithm possible, it's hard to imagine one that would beat Bubble Sort. And yet for years, every computer programming textbook taught this algorithm that's useful for basically nothing, and isn't even intuitive. Students normally react with "How does that even work???" But you know that algorithm made its way into more than a few production systems.

      Software optimization employs some very specific techniques. Notably, using some kind of profiler to identify where your bottlenecks are, and looking for ways to reduce execution or loop counts, or ways to reduce the time spent in each iteration. There's a whole lot of software, including decoding algorithms, that never went through any kind of proper optimization analysis.

      I agree, it's not surprising to find ways to increase performance by 90+%, regardless of the language chosen.

      • by Entrope ( 68843 )

        If you tried to build the most inefficient algorithm possible, it's hard to imagine one that would beat Bubble Sort.

        Challenge accepted [wikipedia.org].

        • Funny! Well, since this sort compares its slowness to Bubble Sort, it would seem that Bubble Sort might still get 2nd place for slowest!

        • by vux984 ( 928602 )

          Bah - I like random sort, which is essentially:

          swap two elements at random
          check if the list is sorted now
          repeat if the list is not sorted

          Given enough time, it will sort the list, quite by accident. ;)

          • If truly random, it's not guaranteed to ever finish.
            Or if your random number generator is broken (worst case it always returns the same number), then it certainly won't finish.

            • by vux984 ( 928602 )

              If its truly random, its not guaranteed to finish in any particular finite amount of time. But given infinite time it will finish.

              It truly is a terrible a sorting algorithm.

              Unless the random number generator is broken, as you say, but most algorithms fail to finish if something crucial is broken.

              As an aside, It can be improved quite a bit by having it only swap the two elements it randomly chooses if the 2nd one is smaller than the first, but it's still worse than bubble sort.

              • In a real world human sense I agree that given infinite time, a decent RNG would eventually work but I'm pretty sure it's not mathematically provable because hey maybe just maybe even in infinite time it doesn't eventually hit it just right.

                It's been a long time but I recall one of my CS professors addressing this and saying something about algorithms must be provable, repeatable, etc, etc, and if it relies on RNG to perform decisions or steps in this manner it is not a proper mathematically correct algorit

      • by ShanghaiBill ( 739463 ) on Monday November 04, 2024 @06:54PM (#64919895)

        If you tried to build the most inefficient algorithm possible, it's hard to imagine one that would beat Bubble Sort.

        Then you lack imagination. Bubble sort is O(n^2). There are O(n^3) sorting algorithms. Here's an O(n!) sort:

        1. Shuffle data randomly
        2. Test if it is sorted. If yes, you're done, else go to 1.

        And yet for years, every computer programming textbook taught this algorithm that's useful for basically nothing

        Bubble sort is useful for very small datasets, like 10 or so, and constrained memory or cache capacity.

        But bubble sort is most taught as an example of a naive implementation leading to poor performance.

        isn't even intuitive. Students normally react with "How does that even work???"

        It's obvious why Bubble sort works. It is way easier to understand than Quicksort.

      • Remember Bubble Sort? If you tried to build the most inefficient algorithm possible, it's hard to imagine one that would beat Bubble Sort. And yet for years, every computer programming textbook taught this algorithm that's useful for basically nothing, and isn't even intuitive. Students normally react with "How does that even work???" But you know that algorithm made its way into more than a few production systems.

        Bubble Sort sorts an already sorted list in O(n) time. Try doing the same thing with Merge Sor

      • Remember Bubble Sort? If you tried to build the most inefficient algorithm possible, it's hard to imagine one that would beat Bubble Sort

        Bubble sort is faster than Quicksort for less than 8 items. That sounds like nothing, but then you realize many if not most sorts done probably have fewer than 8 items.

      • by thegarbz ( 1787294 ) on Monday November 04, 2024 @08:03PM (#64920037)

        And yet for years, every computer programming textbook taught this algorithm that's useful for basically nothing, and isn't even intuitive.

        You didn't pay any attention in class. Bubble sort is held up in virtually every textbook as an example of something that does the base line job in an inefficient way. It is literally taught as an example of not being useful.

        That said I think your assertion that it isn't intuitive is quite silly. It's probably the most intuitive algorithm that is. Is current value bigger than next value in array? If so, switch, repeat, done. There is literally nothing more intuitive than comparing two numbers and just moving the bigger one up the pile to sort something.

        • Perhaps where and when you went to school, they taught bubble sort as an example of inefficient programming. But maybe the 1970s were before your time. In those days, it was presented simply an example of how to sort.

        • Yeah, I've been thinking that if you assign an inexperienced programmer, like a student, to sort a list without any training, you're probably going to get something like bubblesort.

      • by AvitarX ( 172628 )

        I learned bubble sort as a functional example of a while loop.

        It is a super easy to understand program that does something useful and demonstrates some basic functions. We were also told it was pretty useless in real life.

        I don't know why you think it's hard to understand, except for maybe that it's pretty much the first thing taught. It was meant to be hardish to understand but understandable at the point it was taught (like first week or so).

    • Mostly I have used assembler to do stuff needed in a system, stuff that a general purpose library does you on a full operating system. But in an embedded system you are the full operating system, and the RTOSs out there don't give you system startup code and the like. For instance, cache invalidation instructions, interrupt/exception handlers, memory barriers, context switching, etc. Other times you _know_ the code is very slow and can be sped up, and can't easily be sped up with pure standard C code.

      In

    • I think the bigger deal is that an open source project pulled this off.

      What makes open source work is you can use all those libraries to put together incredibly useful and complex software but the downside is you can't just throw a few million dollars at paying some guy with a masters or a PhD in mathematics and computer science to do cool shit for you.

      Getting these kind of optimizations done in an open source framework is super cool.
      • by dryeo ( 100693 )

        A lot of the contributors to FFmpeg are paid by various organizations. It's a big enough and well used project that companies want their hardware to work with it or in the case of such as Google, want their videos smaller and higher quality. Being mostly (by default all) LGPL helps as well.

    • 90% is a good start. But these guys got 9400%. Two factors of magnitude is the difference between get a bruise and sending your head across a football field.

      Yeah I know, you meant 90x. Still it's a pretty major improvement for a tool that lots of people use, unlike your code...

    • Wash, rinse, repeat.

      I remember I inherited some code with all kinds of these hand-tuned SSE things, luckily guarded by ifdefs. Turned them off, and boom, my code was nearly 2X faster. Because SSE had gone the way of the dino, and the compiler was able to take the straight code and do better with newer/wider SIMD registers.

      Hopefully AVX-512 will be around long enough to actually matter for them; however I have my doubts because I saw in other comments Intel is dropping support for it in some CPU families.

    • Ayup, I have on occasion recoded complex military DSP schtuff in assembler and achieved speedups of thousands of times - 10,000x in one case. I have absolutely no desire to go there again, thank you.
    • The layer upon layer is to make coding easier. A side effect is that it makes everything run slower and bloatier, but not problem, because Moore's law makes the hardware smaller and cheaper. Or at least that was the theory.

  • by Xylantiel ( 177496 ) on Monday November 04, 2024 @06:19PM (#64919795)
    I fiddled with this a bit once and it seemed that not all chips implemented "actual" AVX-512. i.e. some chips just support the instructions, they don't actually have the hardware to do all those operation in parallel. Maybe that is discussed more in the article.
    • Yes. Zen4 and especially Zen5 will benefit greatly. Intel dumped AVX512 support on their desktop CPUs with Alder Lake, so unless you're using a Xeon or something you will won't be able to use that codepath.

  • by Anonymous Coward on Monday November 04, 2024 @06:47PM (#64919873)

    A while back, I was working at a place that did video production. They had these expensive, barely working video appliances that the license fees were just plain extortion, and the support was often, "buy our newer model, and we might fix that". I took the physical appliance, removed the disk with the vendor OS and set it aside if need be, installed Linux and used ffmpeg for everything that appliance did. It worked perfectly, and did what we needed it to do, and might as use the Supermicro hardware that the appliance was on for something, since the studio paid multiple orders of magnitude of what it cost.

    Wish that place donated to the ffmpeg project because they saved insane amounts of money after stopping all transactions with the craptastic vendor.

  • Crappy summary. (Score:4, Informative)

    by msauve ( 701917 ) on Monday November 04, 2024 @07:11PM (#64919933)
    Since the summary couldn't be bothered, AVX-512 is an instruction set extension for X86 processors.
  • by godrik ( 1287354 ) on Monday November 04, 2024 @08:16PM (#64920053)

    AVX-512 should not be able to give you a 94x speedup. What are they comparing against, -O1?
    Using AVX-512 should make you go from processing 32bit at a time to processing 512 bits at a time. That should give you at least a 16x speedup.
    Depending on architectures, you can get up to 3 ports of the processor filled with instructors, but that only brings it to 48x speedup. To get to get to a 94x speedup you'd need twice that. So not only you fill all three ports which the baseline couldn't do, but you also send these instruction twice as often with some unroll or something like that.
    (Though you never actually get a 16x speedup on a vector port because in practice most processors have to downclock to have enough power to feed the 512 bit vector units. )
    That also assume that the calculations are entirely instruction bound and not IO bound (including memory bound).

    I do this shit for a living. I highly doubt the 94x speedup of avx-512 against a reasonable compiler with the appropriate compilation flag on a reasonable C implementation of the code. It is not that hard to write code that compile decently well. Even adding a littlebit of unroll and compiler instrinsic in important loops can give you significant performance improvement over -O3.
    Getting a 94x improvement over decent code using handwritten assembly should not be possible. The last time I saw results like that was during the Cell era when compilers didn't know anything about the SPE except the ISA. I have to guess that the baseline here is something unreasonable like "gold standard compiled -O1".

    Do someone has the paper that is being presented here?

    (Though kudos for handwriting writing AVX-512 production code. That's not easy.)

    • by edwdig ( 47888 )

      I would imagine there's also improvements like better memory alignment to avoid cache misses. Maybe more inlining of code, avoiding function calls and branching.

      They're probably doing broader optimizations than you're thinking.

      • by godrik ( 1287354 )

        all of that is totally doable in regular C code. You don't need to handwrite assembly to get there. And pretty much all of that is a generic optimization so you would want that to be part of your baseline.

      • function calls under standard abis are costly and always were, but modern compilers seem to gladly inline anything you tell them to (even languages like c#)

        When to inline is not a solved problem and we know its not a solved problem because compilers never do the opposite, they never find expressions the programmer repeated many times and turn that expression into a function. Where would you even begin with trying to automate such a thing fruitfully? We dont know the science.
    • 32bit to 512bit = 16x theoretical speed up... per Core?? Because 8 cores at 1x vs 8 Cores at 16x = 120x theoretical performance increase... and that's just an 8 core CPU?
    • yeah the reported range was 3x-94x.

      obviously the 94x was on some weird edge case that happens to hit the sweet spot of their optimization. it might even be some 16x16 video they created as a test input for their pipelining.

      most people would just ignore it and focus on the 3x which is still good, but you do you.

    • Presumably they are using AVX-512-BW with 8-bit elements, which gives you 64x. To get past 64x, the baseline C code must be doing something that the SIMD could do differently (maybe some branching was replaced with masking) or maybe hand-optimized assembly helped them get past a failing compiler.
  • by st0nerhat ( 2540360 ) on Monday November 04, 2024 @09:30PM (#64920173)
    If you click through to the raw benchmark data youâ(TM)ll see that the âoe94xâ is compared to a scalar code path that is hardly used. The speed up compared to the avx2 code path which what most users have been using for years is between 1.2x-2x.
  • When looking at all the coding that AI is helping with, I wonder more whether use of inefficient generalised libraries should instead become targets for some custom Assembly to replace oft used libraries and AI should be helping with that specifically... is this a common use case already, IANAD (I'm not a developer).
    • by ledow ( 319597 )

      No, LLM (the type of AI you're talking about) can't do things like that. They are mostly glorified autocomplete tools based on existing code in their training database. Ask them for anything complex and they fail miserably. There's a video for "minecraft created in AI"... watch it. It's basically useless.

      These kinds of things require insight and inference, which no AI is capable of and certainly not LLMs. An LLM is designed to say what you think it should say next. It's literally that simple. And that

  • imagine what a Beo... nah, pardon me

    imagine what CUDA or OpenCL can do.

  • (Not that kind, but you know what I mean.)
  • Was get the handwriting recognition framework working. After that, we just wrote all our code on napkins. The efficiency improvement is remarkable.
  • It's just a selected small function that is exactly the sort of code that would benefit from SSE/AVX etc. It doesn't help that much in the grand scheme of things, I'd say the FFmpeg performance is less important too when 99% of the CPU time is the video encoding library, so that would be libx264/265 for example. It's just a screenshot of a slide of somebody's presentation so I can't tell where this function is.
    Oh and before you say it's Intel propaganda, AMD seems to be doing AVX512 even better than Intel n

  • If you follow the benchmark results link it goes to an image posted on twitter, and in that discussion they clarify that the section in question is part of the decoder. We can all move along now.

To stay youthful, stay useful.

Working...