Search Unity

Burst compiler: deterministic floats

Discussion in 'Burst' started by PaulNK, Mar 21, 2018.

  1. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    27
    I was just reading the ECS docs and noticed that the Burst compiler can create deterministic code. Would this also apply to engine code e.g Vector3, path finding, physics? I’d like to try making an RTS game and it sounds like networking with deterministic lock step simulation is currently being worked on.
     
    ModLunar likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The principles and vision document is about our future direction. Burst does not support deterministic cross platform code execution at this point.
     
  3. AurelWu

    AurelWu

    Joined:
    Oct 11, 2013
    Posts:
    26
    I love the ambition to have deterministic floating point operations on all platforms. However as this seems to be quite some time in the Future, I think it would be great if Unity.Math could include some fixedpoint types in the mean time. I know that there are lots of fixedpoint libraries for c# out there but the beauty of having it integrated would be that it could have 100% consistent naming and methods with float,float2,float3,float4 and once the support for deterministic floats is there it would be super easily refactorable.from fixed to float.
     
  4. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    148
    @PaulNK When they announce that their floats are deterministic, this should automatically include structs like Vector2, Vector3, etc, as these are just using floats.

    @AurelWu I totally agree with you. I would love to see a standard fixed point math library for c#. I read into this topic once for quite a while, and I came to the conclusion that implementing a fixed point math library is the safest way in terms of determinism. 1+1 will always be 2. period. It's not just 100% safe, it's very fast too, including the conversion from fixed to float.

    I am curious how unity wants to achive floating point determinism. It's a heavilly discussed topic, and there are lots of problems to be solved / factors to be terminated. Cpu architecture, operating system, compiler settings. I'm not a pro on this topic but I think they will do something like the IEEE 754 strict mode for the compiler (which will bring a significant performance drawback for all float calculations) but even with this, there is a chance that eventual thirdparty libraries / assemblies which do not implement that setting can blow the whole thing up.

    As I understand, a 100% guarantee is not possible, depending on your usecase. For example If you stick to Windows 10 on 64bit only, it's very unlikley that you ever run into problems with float determinism, even with different cpu architectures, but still no guarantee. There are some games out there which rely on floating point determinsim but you have to be very careful if you do so.

    Regarding lockstep simulation, I would run that whole thing in a separate c# assembly (and thread too) anyway, because you accidentally could bypass the lockstep command wrapper and access / manipulate values directly which will lead to async sim states. You won't be able to benefit from unity libraries like the new job system, at least not in the simuation (still in the game itself) but that shouldn't matter at all. The simulation runs in a slower tickrate and if you are going the datadriven design all the iterations will be very fast due to hardware prefetching. I also like the clean separation of 3D engine and simulation, you will never mix up things and it stays very maintainable.
     
    retypeNickEA likes this.
  5. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    It is a bit early for me to share details because we are still studying all the options and figure out what the best approach is. But yes, cross-platform floating point determinism is a huge beast to fight.
    That said, we have full control over the whole pipeline, from the input code to the binary that gets generated. On top of that, we have a restricted context to apply determinism to, which helps to reduce the options.

    More in general, what we are aiming for is a solution to make Burst-compiled HPC# code determinist cross CPU. If a game uses code that is not Burst-compatible or uses native plugins, then we won't be able to guarantee determinism. Within that restricted context, we have a few solutions that allow for the best performance possible while retaining bit-wise equality in the results of the calculations.

    It is important to note that, in general, you don't need every single floating point calculation to be deterministic. But we will make sure that all the future system that we will ship, will be Burst-compatible and thus deterministic by default. And provide tooling on top of Unity to validate determinism and help debug issues.

    It will take a bit until we reach that point, but that's definitely our goal and all our internal experiments are extremely positive.

    We will share more in-depth information moving forward, as soon as the approach will be finalized.
     
  6. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    738
    Greetings, I am researching about Determinism in Unity, and came across this topic.

    We are creating an RTS based on what is available to us back then.
    Starting from Fixed Point based float, vector2/3, maths, and physics.

    We need a clear yes/no to decide on our next steps.

    1. Will burst compiler provide cross platform determinism?
    2. Will we be able to enjoy the benefits of ECS, Jobs, and Burst with our own custom fixed point library?
    3. When is ECS planned to ready for production? Will it be deterministic when ECS is out of preview?
    4. Is the new DOTS Unity Physics deterministic?

    Thank you!
     
    Last edited: Apr 26, 2019
    meanmonkey likes this.
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    1. Yes the burst compiler will support cross platform floating point determinism end 2019 / early 2020.

    2. You can use fixed point math with ECS / Jobs / Burst. Since all our own code like physics / transforms / AI / Animation will use floating point math it obviously requires you building a lot of the more foundational code yourself if you use fixed point math.

    3. The Unity.Entities package should be out of preview end of this year. But of course all kinds of packages that depend on it might still be in preview, like dots animation, dots physics, dots AI, dots cinemachine etc.

    We have customers who build games in production on top of preview, it means you need to have a certain tolerance level to us changing API's while you build your game.

    4. It deterministic but not yet cross platform deterministic since burst does not support cross platform float determinism yet.
     
    ModLunar, Menion-Leah, Rup3rt and 7 others like this.
  8. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    738
    Thank you for the clear answer, it definitely helped us decide how to proceed our next steps.

    DOTS seems to be everything we are looking for, given all the benefits of safe multi threading and Burst compiler level optimization, it is unfortunate that we already have invested a lot in the foundation code built for determinism from scratch. It became pointless to have a deterministic library when everything built with Burst will already be deterministic.

    However, with these news, I can't help but look forward to the future of Unity with passion, I wish you all a good future ahead!

    Thank you.
     
    ModLunar, meanmonkey and FROS7 like this.
  9. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    148
    I guess it depends on what you are simulating. If you really need deterministic / accurate / complex 3D physics, I think you should dare the switch to floats.

    Despite mass unit / lockstep sims, you will need cross-platform determinism rather for stuff like replay-features in racing games, for example. As for mass unit / lockstep sims, depending on your maximum unit count to be simulated, I would do custom 2D based / faked physics anyway (collision detection, steering, pathfinding based on 2D maps, etc), as doing this with 3D methods will cost you much more performance. But if you really need accurate 3D physics in your mass unit / lockstep sim for whatever reason, then floats using unity is the solution.

    I also would wait for the final solution of unitys float determinism, I guess there will be a performance drawback (even if it's very fast with all the nice unity performance features) when running the burst method in deterministic mode (at least I guess there will be compiler options), so again, depending on your needs, your fixed point math solution (using burst, too, of course) may be even faster. Also it's not clear yet what operations exactly will be deterministc.

    Remember, only your simulation needs to be deterministic, not the render stuff, at least if you are not creating some kind of scientific simulation where the visual stuff matters too.
     
    Last edited: May 4, 2019
    retypeNickEA and Gekigengar like this.
  10. Rup3rt

    Rup3rt

    Joined:
    Nov 23, 2018
    Posts:
    7
    Hi,

    Now I'm in a similar situation of @Gekigengar and I'm wondering if the cross platform floating point determinism of the burst compiler is now ready? if it's not yet ready, approximately when it should be ready? and, what changes are necessary to be able to use a fixed point math library in the DOTS workflow to achieve determinism?

    Any help will be really helpful :D
    Thanks!
     
  11. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Coincidentally I spent the end of last week working on a fixed point library for something, you're welcome to have a look and use it as a starting point.

    https://gitlab.com/tertle/com.bovinelabs.fix32
    • 32 bit
    • works with burst
    • it's far from complete, wasn't intended for the public (at least yet)
      • no documentation
      • it's only got methods i needed implemented atm (no sqrt, sin/cos/tan yet)
    • based off the popular fix64 library https://github.com/asik/FixedMath.Net
    • you can change number of decimal bits and it should work fine
      • public const int DecimalBits = 12 in fix.cs
      • note as it's only 32bit decimal places are limited. the default 12 only leaves a range of +- 500k
    • includes a fix2 (float2) and a mathfix with a few methods
    • unlike the library it's based off, will throw exceptions if an overflow occurs (rather than return an overflow value)
    • includes tests
    • can turn off overflow checks with FIXMATH_NO_OVERFLOW
    • can turn off rounding with FIXMATH_NO_ROUNDING
     
    Last edited: Sep 24, 2019
    retypeNickEA, Deleted User and PaulNK like this.
  12. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    @Joachim_Ante: do you know if there is any plan to develop a new DOTS-based version of Unity's 2D Physics engine as well? Thank you
     
  13. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    Is there any progress on this? It's been 2 years since this was proposed and I haven't found any other updates on this feature.
     
    Menion-Leah likes this.
  14. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    It was discussed on GitHub recently, see this issue.
     
    Lukas_Kastern, jdtec and Menion-Leah like this.
  15. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    I'm starting to think fixed point is the the way to go more these days when determinism is a requirement. I think Unity could maybe get deterministic floats eventually (although based on recent posts it doesn't sound like it's a focus anymore?) but I can see it being dropped in later years due to continued dev maintenance cost & keeping up with evolving hardware. Or maybe I'm wrong and someone knows better? :)
     
  16. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    Haha, I was the one who filed that issue. Commenting here for a bit more visibility.
     
    Lukas_Kastern and nxrighthere like this.
  17. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    For those who are looking for a working solution without needing compiler support one of my fellow devs found this repo https://github.com/RomanZhu/FixedPoint-Sharp, which supports a Unity.Mathematics style use of Q48.16 fixed point values (fp, fp2, fp3, fp4),seems to be well tested, and seems to be optimized for performance (plenty of manual inlining and other micro-optimizations). It however currently lacks an equivalent Matrix4x4 definition and related operations and obviously doesn't have Burst compiler support (no SIMD optimizations).

    This does however mean needing to build atop of it the other related systems sometimes needed in deterministic simulations (physics, animation ,etc) and would require bifurcating existing systems to support it.
     
    SenseEater, msfredb7 and jdtec like this.
  18. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Thanks for sharing @james7132. That may come in use in a year or so if there's still no Unity mathematical determinism around.
     
  19. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    I just read the Unity 2021 Official Roadmap; unfortunately, there's no reference to DOTS cross-platform floating point determinism.

    Is this feature eventually abandoned?
     
  20. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It is still planned.
     
  21. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    That's excellent news, thanks!
     
    Gekigengar likes this.
  22. zheyouchen

    zheyouchen

    Joined:
    Jul 16, 2020
    Posts:
    1
    Cant wait to see a cross platform deterministic compiler even it is a very early access
     
  23. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    It's been over 3 years since the start of this post, and still no sign of this feature landing in any near or even far future of Burst.

    Things outside of Unity has also moved quite a bit since then. There is now a Rust-based physics engine called Rapier (https://rapier.rs/) that purports to provide deterministic results on all platforms that are IEEE 754-2008 compliant (read: pretty much every desktop processor since the early 2010s). A redditor dug into it's implementation (https://www.reddit.com/r/Unity3D/comments/lkxb9d/crossplatform_deterministic_physics_with_unity/) and found that the use of Rust's port of musl C's libm provided in-software floating point operations that are guaranteed to be IEEE 754 compliant in a cross-platform way. They then took the existing physics package and made it work across a desktop Editor, WebGL, and Android.

    This of course incurs a 4x performance loss due to the failure to vectorize and other compiler optimizations, about on par with using fixed point calculations, but it allows for operating on a wide range of scales and retains binary compatibility with other floating point computations and thus does not incur conversion costs when entering and exiting deterministic code. This performance loss is unavoidable for cross platform determinism, as SIMD instructions hardware implementations may not be identical (Intel vs AMD) nor IEEE 754 compliant.

    Even if it may not be possible for all of Unity's supported platforms, it'd be nice to at least have FloatMode.Deterministic just substitute all floating point operations with these "soft floats" instead. Since Burst requires going through an IL2CPP step, it seems trivial to introduce this step: create a C++ type for soft floats and just use it in place of float during C++ codegen. Perhaps fail compilation when the target platform is not IEEE 754-2008 compliant or add a runtime initialization check for it.

    Additionally, a define flag for changing the default FloatMode.Default to Deterministic instead of Strict may be needed as developers will need to fork existing packages just to update the FloatModes on system jobs (as the redditor above did) to get the desired determinism.
     
    Last edited: May 3, 2021
    jdtec and Menion-Leah like this.
  24. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    417
    Yes, this feature has been delayed because we have higher priority coming directly from many customers. The top priority we have today is iteration time where many customers have hundreds/thousands of jobs to compile and Burst (and other components in the full code editing refresh cycle) doesn't scale when a small change is occurring in their codebase.

    We have made a good prototype of Burst that was actually already giving good results between ARM and x64. But, the reality is that determinism is not only about IEEE, there are lots of code related to multi-threading that would need be to be revisited for example (e.g because the aggregation, timing would impact determinism). For example, in .NET (CLR) if you add string keys to a dictionary, iterating over the keys might not always be the same between consecutive runs. Bringing determinism to Burst is actually the least of the trouble, we have a lot more challenges regarding actual engine and user code, .NET in Mono and IL2CPP. So the topic is not really as simple as "just do soft float and ship it", the reality is a lot more complicated.

    Burst does not require IL2CPP, it can run with Mono, IL2CPP and .NET Core. Again, it is trivial for a helloworld "a+b" to get the same IEEE results, we have already that working with Burst. It's not where lies the complexity of this problem.

    But we do want to tackle this problem in the future, it's on our mind and in our roadmap, but it's not the priority right now. We agree that it's unfortunate that we advertised it as "up-coming" at the beginning of Burst (and I may have contributed myself to this), while we didn't know what would be the future (usage) of Burst.

    Today, Burst is used by many critical customers, often beyond our expectations in terms of scale, and what they are asking us is a fast and good iteration/development experience (in addition to debugging). That's what all the team is focused on this year.
     
    Last edited: Apr 30, 2021
  25. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    Thanks for the swift response!

    Non-determinism as a result of multithreaded code or use of non-determinsitic data structures is a given, but developers can work around that, but not the compilation results provided by Burst. Getting some version of FloatMode.Determinstic out would already make a world of difference for those who were developing or experimenting with Burst with the expectation that the feature would land soon. I certainly have developed everything else needed short of actually having determinsitic floats while waiting for the feature to land. If it takes another year or three to have this feature land, I think the time would have been better spent using a different engine or developing my own from scratch.
     
  26. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    218
    First of all thanks for the update on the current status and priorities, it's incredibly useful to know what's happening currently and in the near future!

    I'm basically just commenting to hopefully bump up floating point determinism a tiny notch on the priorities list since we're also waiting for it and it's pretty important to us.
    We have a bit of experience with determinism in Unity due to the lockstep multiplayer mode in our game Parkitect, and as james7132 says non-determinism in some parts is usually not a big problem and can be worked around.
    Having just deterministic floats alone would be huge already.
     
  27. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    I typed out a response and deleted already but what the hell, here's the jist of it again.

    +1 What James7132 said

    This thread is about float determinism in burst, not about all of Unity engine being deterministic. Games have been launched and developed before in Unity working around the other issues you mentioned using fixed point. The issues you mentioned sound like a different topic to the thread title, unless I'm ignorant of something here?

    That said I'm glad you are prioritising debugging and iteration time as these are the biggest pain points when using Unity for me currently. I've already switched to fixed point maths & physics and it's actually nicer in a way as I have more control of things now.

    Appreciate you responding with an update, xoofx.
     
    james7132 likes this.
  28. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    What James7132 +1000 I can work around any other level of non-determinism. I am in the middle of developing a large scale RTS. I can even write my own physics but I really need floating point determinism.

    Fixed point is kind of meh trigonometric functions suffer. When I started working with ECS this feature seemed imminent. Now I have this bad feeling that floating point determinism may never happen.
     
    Last edited: May 14, 2021
  29. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    From what I've seen of most fixed libraries, they just precompute all values up to a certain precision to avoid this issue. Think the issue I had with a fixed library was Sqrt.
     
  30. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    I have had problems with trig functions being jagged enough that stuff does not look smooth.
     
  31. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    For those interested, I have been experimenting with offloading computations to a compiled Rust binary, since Rust's f32 type is guaranteed to be IEEE-754 2008 compliant on all platforms, utilizing nalgebra and libm to cover operations not covered by the spec.

    I'm getting bitwise identical results on every platform that I'm testing on so far (just Windows, Mac, and Linux right now), though I have yet to try to integrate this with a Burst compiled job, and I'm not sure how performant it is given the potential FFI interface it has to go through.

    With this I might move towards just using Unity only as a rendering/audio/authoring tool, and just move all game simulation logic and state to a compiled Rust plugin, or just move off of Unity entirely.

    For those looking for a pure C# solution, the aforementioned soft-floats should work, though you might need to fork all of Unity's packages and just replace everything with sfloat.
     
    Iron-Warrior, Menion-Leah and jdtec like this.
  32. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
  33. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    Software floats would be great. I am not sure if they plan to go that way or not.
     
    Menion-Leah likes this.
  34. Elfinnik159

    Elfinnik159

    Joined:
    Feb 24, 2013
    Posts:
    145

    When I was looking for an alternative to float for determinism, I saw this post among others. As far as I remember, it has a repository of reworked Unity physics? If this is so and I am not confusing anything (because about six months ago I tried several similar solutions), then when tested in the editor, this library was 2-20 times slower (with different operations) than float. And that's acceptable, I think determinism is worth it.

    However, the build profiling showed the following:
    Common operations (+, -, etc.) are 2-10 times slower than using float
    Some operations, such as square root, are several thousand times slower than float.
    These numbers are roughly the same (or worse) for all the libraries I've tested.

    Vector normalization is a common operation that uses square root (or inverse square root, but that seems to be outside the scope of such libraries). Slowing down this operation ten thousand times is too significant.

    These are the results of tests half a year ago, everything could have changed, and I could be wrong somewhere.
     
    TheOtherMonarch likes this.
  35. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    Thank you so much for sharing your experience! I totally agree with what you said.
    Only thing I hope is that there could be a significant margin of improvement, especially because (IIRC) some operations (IE: division) had been implemented by the aforementioned developer himself, and it was his first experience with such things.
     
  36. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    If software floats are ten thousand times slower they are not viable. Determinism has been done other ways for a long time.
     
  37. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    I have tested the latest ECS build with burstcompiler, surprisingly, it is very different from my previous test back in 2018. Currently, I were able to get same results test via Arm64 and X86-64 architecture through IL2CPP burst compiler, PC editor will skip il2cpp using mono burst compiler, thus still have different results. What is the exact status on this, is there any loop hole that I might run into? Also what if I had a json file with bunch of floats, then read those floats into ECS during runtime and using those floats modify physics force. Would this affect determinstic result?
     
  38. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Determinism in Burst is still very much in experimental/preview state - we can't guarantee that it'll produce deterministic results. Also, if you are processing floats anywhere that is not in a Burst-compiled job with deterministic math, then you could get different results.

    For reading floats from a JSON, it is possible to get last-unit precision differences depending on the string -> float changes, so I'd be wary there.
     
    msfredb7 likes this.
  39. xAdamQ

    xAdamQ

    Joined:
    Jul 23, 2016
    Posts:
    53
    Hello, as of today, I see dots physics 1.0 is released. Is there any updates regarding deterministic physics in unity generally?

    version 1.0 documentation mentions determinism everywhere, which is great
     
    Last edited: Oct 11, 2022
    Thalmus, zhuchun, GameFinder and 2 others like this.
  40. TechCenterUser

    TechCenterUser

    Joined:
    Jun 8, 2020
    Posts:
    5
    xAdamQ likes this.
  41. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    Looks dead last commit was on Mar 1, 2018. Fixed points are a bad solution you only see appearing in C# code bases.
     
  42. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    163
    Can you elaborate? What to you suggest instead for deterministic math?
     
  43. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    What I am attempting to convey is that in C++ or Rust people use deterministic floating points, not fixed points. It is only in C# where we see a return to fixed points. Because of the limits of managed code. Bust is native code so we should not have to rely on fixed points.

    Fixed points bring tons of issues that are very hard to solve.
     
    msfredb7 likes this.
  44. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    163
    I see, thanks for clarifying.

    Correct me if I'm wrong but wouldn't that mean that, unless 100% of gameplay code can be bursted, fixed point is still required for determinism in c#?
     
  45. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    Yes your sim code needs to be bursted. Nonsynchronous code does not have to be. If you are using ECS physics it should not be a big issue.