News for hackers. Stuff that matters.
Re:Stable Diffusion in pure C/C++ by galangalalgol on Saturday August 19, 2023 @10:09AM (#37188953)
As long as we are language trolling, why would anyone start a greenfield project like this in C++ these days? The android, windows, firefox, and now chrome projects have all begun to shift towards rust and in the case of Android and Firefox, write a significant amounts of their project in rust. Migrating an existing project like that is difficult. The chrome team in particular lamented the difficulty. But starting a new project? If you have a team familiar with performant c++ the speedbump of starting a greenfield project in rust is negligible, and the ergonomic improvements in the build system and the language itself will make up for that in any project that takes more than a few months. For that speed bump you get memory and thread race safety, far better than any stack of c++ analysis tools could ever provide with a tiny fraction of the unit tests you'd write in c++. And you lose no performance.
Re:Stable Diffusion in pure C/C++ by mnrlt on Saturday August 19, 2023 @10:48AM (#37189353)
C++ has a standard, multiple competing implementations and a largely drama-free community.

Does CUDA even have Rust bindings, and if so, are they on the same level as the C++ ones?

What do you mean by "the windows projects" that shift towards Rust?

Re:Stable Diffusion in pure C/C++ by galangalalgol on Saturday August 19, 2023 @11:41AM (#37189870)
MS has started implementing pieces of windows in rust. If you have windows 11 you are running rust. The cuda bindings are good for ml, but missing for cufft and similar. There are people working on better cuda support, but there are even more people working on vendor agnostic gpgpu using spirv and webgpu. It isn't there yet. Right now you are mostly left to your own bindings unless you are doing ml or blas.
Re:Stable Diffusion in pure C/C++ by pjmlp on Saturday August 19, 2023 @03:20PM (#37192148)
Yes they started, yet there is enough C++ to rewrite in the 30 years of Windows NT history.

Meanwhile, Visual Studio team released better tooling for Unreal in Visual C++.

Re:Stable Diffusion in pure C/C++ by Const-me on Saturday August 19, 2023 @10:49AM (#37189368)
> why would anyone start a greenfield project like this in C++ these days?

TLDR: quite often, using C++ instead of Rust saves software development costs.

Some software needs to consume many external APIs. Examples on Windows: Direct3D, Direct2D, DirectWrite, MediaFoundation. Examples on Linux: V4L2, ALSA, DRM/KMS, GLES. These things are huge in terms of API surface. Choose Rust, and you gonna need to write and support non-trivial amount of boilerplate code for the interop. Choose C++ (on Linux, C is good too) and that code is gone, you only need well-documented and well supported APIs supplied by the OS vendors.

Similarly, some software needs to integrate with other systems or libraries written in C or C++. An example often relevant to HPC applications is Eigen. Another related thing, game console SDKs, and game engines, don’t support Rust.

For the project being discussed here, GGML, for optimal performance the implementation needs vector intrinsics. Technically Rust has the support, but in practice Intel and ARM are only supporting them for C and C++. Not just CPU vendors, when using C or C++ there’re useful relevant resources: articles, blogs, and stackoverflow. These things help a lot in practice. I don’t program Rust, but I program C# in addition to C++, technically most vector intrinsics are available in the current version of C#, but they are much harder to use from C# for this reason.

All current C and C++ compilers support OpenMP for parallelism. While not a silver bullet, and not available on all platforms supported by C or C++, some software benefits tremendously from that thing.

Finally, it’s easier to find good C++ developers, compared to good Rust developers.

Re:Stable Diffusion in pure C/C++ by galangalalgol on Saturday August 19, 2023 @11:08AM (#37189550)
There are existing supported bindings for direct3d from MS, as they themselves are migrating. GLES and ggml also have supported bindings. I like nalgebra + rustfft better than eigen now. Nalgebra still isn't quite as peformant on small matricies until const generic eval stabilizes, but it is close enough for 6x6 stuff that it is in the noise. Rustfft is faster than fftw even. Rust has intrinsic support on par with clang and gcc, and the autovectorizer uses whatever llvm know about, so again equivalent to clang.

On the last point, I will again assert that a good c++ developer is just a good rust developer minus a month of ramp, that you'll get nack from not having to fight combinations of automake, cmake, conan, vcpkg, meson, bazel and hunter.

Re:Stable Diffusion in pure C/C++ by Const-me on Saturday August 19, 2023 @11:57AM (#37190039)
I'll be very surprised if MS will ever support rust bindings for media foundation. That thing is COM based, requires users to implement COM interfaces not just consume, and is heavily multithreaded.

About SIMD, automatic vectorizers are very limited. I was talking about manually vectorized stuff with intrinsics.

I've been programming C++ for living for decades now. Tried to learn rust but failed. I have an impression the language is extremely hard to use.

Re:Stable Diffusion in pure C/C++ by galangalalgol on Saturday August 19, 2023 @12:18PM (#37190305)
Not sure how fully featured it is. https://lib.rs/crates/mmf

Yes, rust directly supports modern intrinsics, that is what rustfft for instance uses. I try to stick with autovec myself, because my needs are simpler such that a couple tweaks usually gets me close to hand-rolled speedups on both avx 512 and aarch64. But for more complicated stuff yeah, rust seems to be keeping up. Some intrinsics are still only in nightly, but plenty of major projects use nightly for production, it is quite stable and with a good pipeline you'll be fine.

I've written c++ since ~94, and mostly c++17 since it came out. About a quarter of a century of that getting paid for it. I never liked or used exceptions or rtti, and generally used functional style except for preallocation of memory for performance. I think those habits might have made the transition a little easier, but the people on my team who had used a more OOP style and full c++ don't seem to have adapted much more slowly if at all. I struggled for years to internalize rust at home until I just jumped in at work by declaring the project I lead would be in rust. I have had absolutely no regrets. It really isn't as bad a learning curve as c++. But we learned c++ one revision at a time. Also, much like c++ rust has bits you mostly only need to know for writing libraries. So getting started you can put those things to the side for a bit right at first.

Re:Stable Diffusion in pure C/C++ by Const-me on Saturday August 19, 2023 @07:46PM (#37194524)
> Not sure how fully featured it is

The crate you have linked contains just a single line of source code. Here it is:

   pub struct SourceReader {}
Media foundation API reference: https://learn.microsoft.com/en-us/windows/win32/medfound/med...

> rust directly supports modern intrinsics

C# also directly supports them, but it doesn’t help with usability. The support alone is not enough, the API needs to match the C compiler extensions defined decades ago by Intel and ARM.

Re:Stable Diffusion in pure C/C++ by theLiminator on Saturday August 19, 2023 @04:36PM (#37192903)
Curious how long you tried to learn rust? I've found C++ much harder to learn (coming from a python/scala) background.

Is it just a case of you forgetting how hard C++ was to learn?

Re:Stable Diffusion in pure C/C++ by Const-me on Saturday August 19, 2023 @07:20PM (#37194349)
I have minimal experience with Rust. OTOH, programming C++ for living since 2000, with a few gaps when I used other languages like Obj-C and C#.

I agree C++ is very hard to learn if you only have experience with higher-level languages like Python and Scala. I think there’re two reasons for that.

C++ is unsafe. There’s no way around this one, it was designed that way, like C or assembly. Still, with modern toolset it’s not terribly bad. Compilers print warnings, BTW I typically ask them to treat warnings as errors to deliberately fail the build. On Windows, a combination of debug build, debug C runtime, and visual studio debugger helps tremendously. Linux compilers have these sanitizers (address, memory, thread, undefined behavior) which are comparable, they too sacrifice runtime speed for diagnostics and debuggability.

Another reason, the language itself is very complicated, especially the templates. However, just because something is in the language doesn’t mean it’s a good idea to use it. You don’t need to be familiar with that stuff unless doing something very advanced, like customizing the Eigen C++ library. Don’t follow the patterns found in the standard library: unlike your code, that library has good reasons to use that template BS. If instead of templates you do something else, C++ becomes much easier to use, and most importantly other people will still be able to read and understand your code. Another reason to avoid excessive template metaprogramming, it slows down the compiler, because template-heavy code often needs to be in headers as opposed to cpp files.

P.S. If you don’t need extreme levels of performance (defined as “approach the numbers listed in CPU specs”, the numbers are FLOPS or memory bandwidth), and you don’t need the ecosystem too much, consider C# instead of C++. Much faster than Python, often faster than Scala or Java, easy integration with C should you need that (about the same as Rust, much easier than Python or Java), the only downside is these ~100MB of the runtime. The reputation is weird, but technically the language and runtime are pretty good. For example, here’s a C# library which re-implements a subset of ffmpeg and libavcodec C libraries for one particular platform, Linux on Raspberry Pi4: https://github.com/Const-me/Vrmac/tree/master/VrmacVideo

Re:Stable Diffusion in pure C/C++ by theLiminator on Sunday August 20, 2023 @02:59PM (#37201970)
Personally, I use rust for the ability to go as fast as C++ but with far fewer footguns. Not to mention that I personally really enjoy how they made algebraic data types and package management first class.

I suspect that if you really spent time learning rust that you'd really appreciate it coming from C++.

Re:Stable Diffusion in pure C/C++ by Const-me on Monday August 21, 2023 @09:41AM (#37209477)
When I just want the ability to go as fast as conventional readable C++, I use C# instead. Compared to conventional C++, C# is typically only slightly worse in terms of performance. C# is safer than Rust, and the usability is IMO an order of magnitude better. Just the compilation speed is already a huge contributing factor.

I use C++ for 2 main reasons, integration with other software or libraries, and performance.

When I need the performance, I don’t write conventional C++. I implement custom data structures, usually use vector intrinsics, sometimes use other platform intrinsics like BMI2, sometimes implement custom threading strategies on top of OpenMP or other platform-supplied thread pools. Most of these things are impossible or very hard to express in idiomatic safe Rust, which means the language constantly gets in the way.

Also, most programs contain both performance critical and performance agnostic pieces. When both pieces have non-trivial complexity, I typically use both C# and C++ for the software. Often the frontend part is in .NET, and the performance-critical backend is in C++ DLL.

Re:Stable Diffusion in pure C/C++ by theLiminator on Wednesday August 23, 2023 @06:31PM (#37242725)
I believe rust is actually more thread safe than C#, though the other points you made are pretty true.
Re:Stable Diffusion in pure C/C++ by pjmlp on Saturday August 19, 2023 @03:22PM (#37192175)
No there aren't, unless you mean Rust/WinRT demos with community bindings.

Agility SDK and XDK have zero Rust support. If it isn't on the Agility SDK and XDK, it isn't official.

Hardly the same as the official Swift bindings to Metal, written in Objective-C and C++14.

Re:Stable Diffusion in pure C/C++ by FoodWThrow on Saturday August 19, 2023 @04:00PM (#37192526)
Rust is great when you know what you're building. That qualifier encompasses quite amount of software space, but not all of it, and I would argue not even the majority of it.

If you don't know what you are doing, if you are exploring ideas, Rust will just get in the way. At some point you will end up realizing you need to adjust lifetimes, and that will require you to touch non-trivial amount of your code base. If you need to that multiple times, friction will overwhelm your desire to code.

I have a pet theory that, the people that find Rust intuitive and fun, are the people that are working on well beaten paths; Rust is almost boring at doing that, which is a good thing. And the people that find Rust gets in their way are the people that like to experiment with their solutions, because there aren't any set, trusted solutions within their problem space, and even if there are, they like to approach the problem on their own, for better or worse.

In any case:

> why would anyone start a greenfield project like this in C++ these days?

The video game industry can single-handedly carry C++ on their back, kicking and screaming, if need be. Rust is uniquely unfit to write gameplay code due to game development's iterative nature. Using scripting languages doesn't cut it either, because often, slower designer made scripts will need to be converted to C++ by a programmer, and pull in the crazy reference hell of the game state into the C++ land.

I would say Rust is OK for engine level features -- those don't change that often, and requirements are usually well understood. But that introduces a cadence mismatch between different systems too, so there is a cost there as well. But for gameplay? There's a reason why many Rust based game engines use crazy amount of unsafe Rust to make their ECS. Just not a good fit.

And of course, there's the consoles, where Sony seem to have a political reason for not supporting Rust on non-1st-party studios. I have no idea what they are thinking, honestly.