diff --git a/Directory.Packages.props b/Directory.Packages.props index 1aad575e5f..9f48677feb 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -54,15 +54,15 @@ - - - + + + 1.5.25 - + - + diff --git a/modules/Nncase.Modules.CPU/packages.lock.json b/modules/Nncase.Modules.CPU/packages.lock.json new file mode 100644 index 0000000000..5dd73e5bda --- /dev/null +++ b/modules/Nncase.Modules.CPU/packages.lock.json @@ -0,0 +1,295 @@ +{ + "version": 2, + "dependencies": { + "net7.0": { + "StyleCop.Analyzers": { + "type": "Direct", + "requested": "[1.2.0-beta.435, )", + "resolved": "1.2.0-beta.435", + "contentHash": "TADk7vdGXtfTnYCV7GyleaaRTQjfoSfZXprQrVMm7cSJtJbFc1QIbWPyLvrgrfGdfHbGmUPvaN4ODKNxg2jgPQ==", + "dependencies": { + "StyleCop.Analyzers.Unstable": "1.2.0.435" + } + }, + "Google.OrTools.runtime.linux-arm64": { + "type": "Transitive", + "resolved": "9.4.1874", + "contentHash": "Z46ndZcZa2Lt5b76xU9kxVYbPLg/LfuMufhUVsu3Qo3L7Bibf7WXd9j7RRldjnuv8RIHWTqb0b+2FwwMxs0c5A==" + }, + "Google.OrTools.runtime.linux-x64": { + "type": "Transitive", + "resolved": "9.4.1874", + "contentHash": "zGeDb8FuvP9HXjrsU7krVXtSDFpR+DUGNEsH51k94jL9tzf2vWYI8+WUBRHZ/cGe50dpLr+vIjfcNo3gFyOpkQ==" + }, + "Google.OrTools.runtime.osx-arm64": { + "type": "Transitive", + "resolved": "9.4.1874", + "contentHash": "Wo0ZfDaH6DhiQw0jZm4HWJm/oPGPpWNwOLUz+EYaoH3MLtocSxItHGQj/Ta3HyhXnYNOv+TliAH8L+8RCXu/2w==" + }, + "Google.OrTools.runtime.osx-x64": { + "type": "Transitive", + "resolved": "9.4.1874", + "contentHash": "IAfGgKR1og6vU87axK1d37Ak/4jy8B4NMoElovG/KZc/2UY+cJEAQDA709UMegtI4lBhuxTWFNUiHQYmRIB9yQ==" + }, + "Google.OrTools.runtime.win-x64": { + "type": "Transitive", + "resolved": "9.4.1874", + "contentHash": "fUs5qDnZA6itygolcX6nPuachQkY9CVvQbakIzIiRAWKcaj8umQAbFdGwbkyzp3qp34BKW5mtPVsmMyfQBBjOQ==" + }, + "libortki": { + "type": "Transitive", + "resolved": "0.0.2", + "contentHash": "svfuG5mxGY/QC/5DVheHOCELmdSP90RtxQ73j23KarPXZ9ZXW+7v1l5J77hGDyQbEh1BGrnGgKBlyn76RauGHg==", + "dependencies": { + "libortki-linux": "0.0.2", + "libortki-osx": "0.0.2", + "libortki-osx-arm64": "0.0.2", + "libortki-win": "0.0.2" + } + }, + "libortki-linux": { + "type": "Transitive", + "resolved": "0.0.2", + "contentHash": "b04LWD4lgGy60tys3hPFhnUpgWDM6dN5r1PI7GOcPj8VupXCaI70LKNQ5/5twbDE6rkowOGanVTw0S2wBGBqBQ==" + }, + "libortki-osx": { + "type": "Transitive", + "resolved": "0.0.2", + "contentHash": "O6Q9GLULkDkZEPAZJVKLPH0ROXGVOE7BxuddgOcHNK2oiTEM7wIRnzp2OIlYgLpaOLyxJMisbGOhtWgdzt2Wng==" + }, + "libortki-osx-arm64": { + "type": "Transitive", + "resolved": "0.0.2", + "contentHash": "4Qn2dirJmRicnUG945oWpq7HVGwgqCKKxYPMISv/MRvmpZBbXrZ1cVvRaF8WwTu4XXgfKTa1sLv+i8zLifUMeQ==" + }, + "libortki-win": { + "type": "Transitive", + "resolved": "0.0.2", + "contentHash": "HAoROgAKn8XBun11X43HZuspKlo5JGy8/OYw5IUPo7FVh5TCaPrLjGmyGYYZ2dqLlv31yv/b6s254PIRGn95cA==" + }, + "Microsoft.Extensions.Configuration.Abstractions": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "qWzV9o+ZRWq+pGm+1dF+R7qTgTYoXvbyowRoBxQJGfqTpqDun2eteerjRQhq5PQ/14S+lqto3Ft4gYaRyl4rdQ==", + "dependencies": { + "Microsoft.Extensions.Primitives": "6.0.0" + } + }, + "Microsoft.Extensions.DependencyInjection.Abstractions": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "xlzi2IYREJH3/m6+lUrQlujzX8wDitm4QGnUu6kUXTQAWPuZY8i+ticFJbzfqaetLA6KR/rO6Ew/HuYD+bxifg==" + }, + "Microsoft.Extensions.FileProviders.Abstractions": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "0pd4/fho0gC12rQswaGQxbU34jOS1TPS8lZPpkFCH68ppQjHNHYle9iRuHeev1LhrJ94YPvzcRd8UmIuFk23Qw==", + "dependencies": { + "Microsoft.Extensions.Primitives": "6.0.0" + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "9+PnzmQFfEFNR9J2aDTfJGGupShHjOuGw4VUv+JB044biSHrnmCIMD+mJHmb2H7YryrfBEXDurxQ47gJZdCKNQ==", + "dependencies": { + "System.Runtime.CompilerServices.Unsafe": "6.0.0" + } + }, + "NetFabric.Hyperlinq.Abstractions": { + "type": "Transitive", + "resolved": "1.3.0", + "contentHash": "WXnEcGwmXfa8gW9N2MlcaPNUzM3NLMwnAhacbtH554F8YcoXbIkTB+uGa1Aa+9gyb/9JZgYVHnmADgJUKP52nA==" + }, + "StyleCop.Analyzers.Unstable": { + "type": "Transitive", + "resolved": "1.2.0.435", + "contentHash": "ouwPWZxbOV3SmCZxIRqHvljkSzkCyi1tDoMzQtDb/bRP8ctASV/iRJr+A2Gdj0QLaLmWnqTWDrH82/iP+X80Lg==" + }, + "System.Buffers": { + "type": "Transitive", + "resolved": "4.5.1", + "contentHash": "Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg==" + }, + "System.Runtime.CompilerServices.Unsafe": { + "type": "Transitive", + "resolved": "6.0.0", + "contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==" + }, + "nncase.codegen": { + "type": "Project", + "dependencies": { + "Extension.Mathematics": "[1.2.12, )", + "Nncase.Core": "[1.0.0, )", + "Nncase.IO": "[1.0.0, )" + } + }, + "nncase.core": { + "type": "Project", + "dependencies": { + "DryIoc.dll": "[5.3.1, )", + "GiGraph.Dot": "[2.0.0, )", + "Microsoft.Extensions.Hosting.Abstractions": "[6.0.0, )", + "Microsoft.Extensions.Logging.Abstractions": "[6.0.0, )", + "Microsoft.Extensions.Options": "[6.0.0, )", + "Microsoft.Toolkit.HighPerformance": "[7.1.1, )", + "NetFabric.Hyperlinq": "[3.0.0-beta48, )", + "System.Reactive": "[5.0.0, )" + } + }, + "nncase.diagnostics": { + "type": "Project", + "dependencies": { + "Nncase.Core": "[1.0.0, )" + } + }, + "nncase.egraph": { + "type": "Project", + "dependencies": { + "GiGraph.Dot": "[2.0.0, )", + "Google.OrTools": "[9.4.1874, )", + "NetFabric.Hyperlinq": "[3.0.0-beta48, )", + "Nncase.Core": "[1.0.0, )", + "Nncase.Evaluator": "[1.0.0, )", + "Singulink.Collections.Weak": "[1.0.2, )" + } + }, + "nncase.evaluator": { + "type": "Project", + "dependencies": { + "Nncase.Core": "[1.0.0, )", + "OrtKISharp": "[0.0.2, )" + } + }, + "nncase.graph": { + "type": "Project", + "dependencies": { + "Nncase.Core": "[1.0.0, )", + "Nncase.Evaluator": "[1.0.0, )" + } + }, + "nncase.io": { + "type": "Project" + }, + "nncase.modules.stackvm": { + "type": "Project", + "dependencies": { + "Nncase.CodeGen": "[1.0.0, )", + "Nncase.Passes": "[1.0.0, )" + } + }, + "nncase.passes": { + "type": "Project", + "dependencies": { + "Nncase.Core": "[1.0.0, )", + "Nncase.EGraph": "[1.0.0, )", + "Nncase.Evaluator": "[1.0.0, )", + "Nncase.Graph": "[1.0.0, )" + } + }, + "DryIoc.dll": { + "type": "CentralTransitive", + "requested": "[5.3.1, )", + "resolved": "5.3.1", + "contentHash": "E3zclUh2CIBks1t2uBD1k18pyGFJ1YSKCrbCDbB7qCdl2RAB+k68AyDpjeplhF1ot2XPV82AgyCWBXMf0ggL1g==" + }, + "Extension.Mathematics": { + "type": "CentralTransitive", + "requested": "[1.2.12, )", + "resolved": "1.2.12", + "contentHash": "D4mn5Cab4ztPLJ0V8uMErDrO/Y61098nwrvyIOLZymVAYOQcwP1vomVWKbTagf1aPU3cX5Q7adZtQEQwOy6XEg==" + }, + "GiGraph.Dot": { + "type": "CentralTransitive", + "requested": "[2.0.0, )", + "resolved": "2.0.0", + "contentHash": "ThvS2mQVveSkTMUm04tMbRYzu1XFPV8xBHISrUMp02APjhv9IRbLu3v3upTPCywORx2Ds/c6AqEUL1WU6kPfuQ==" + }, + "Google.OrTools": { + "type": "CentralTransitive", + "requested": "[9.4.1874, )", + "resolved": "9.4.1874", + "contentHash": "jqRoI+pYlym+fhoU25u+13oti5h+772bllQ9zDitTVMclDXVTiG6pxzvmYO74wnADBMdpb2SQlgiNQxoNk5dlA==", + "dependencies": { + "Google.OrTools.runtime.linux-arm64": "9.4.1874", + "Google.OrTools.runtime.linux-x64": "9.4.1874", + "Google.OrTools.runtime.osx-arm64": "9.4.1874", + "Google.OrTools.runtime.osx-x64": "9.4.1874", + "Google.OrTools.runtime.win-x64": "9.4.1874", + "Google.Protobuf": "3.19.4" + } + }, + "Google.Protobuf": { + "type": "CentralTransitive", + "requested": "[3.19.4, )", + "resolved": "3.19.4", + "contentHash": "fd07/ykL4O4FhqrZIELm5lmiyOHfdPg9+o+hWr6tcfRdS7tHXnImg/2wtogLzlW2eEmr0J7j6ZrZvaWOLiJbxQ==" + }, + "Microsoft.Extensions.Hosting.Abstractions": { + "type": "CentralTransitive", + "requested": "[6.0.0, )", + "resolved": "6.0.0", + "contentHash": "GcT5l2CYXL6Sa27KCSh0TixsRfADUgth+ojQSD5EkzisZxmGFh7CwzkcYuGwvmXLjr27uWRNrJ2vuuEjMhU05Q==", + "dependencies": { + "Microsoft.Extensions.Configuration.Abstractions": "6.0.0", + "Microsoft.Extensions.DependencyInjection.Abstractions": "6.0.0", + "Microsoft.Extensions.FileProviders.Abstractions": "6.0.0" + } + }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "CentralTransitive", + "requested": "[6.0.0, )", + "resolved": "6.0.0", + "contentHash": "/HggWBbTwy8TgebGSX5DBZ24ndhzi93sHUBDvP1IxbZD7FDokYzdAr6+vbWGjw2XAfR2EJ1sfKUotpjHnFWPxA==" + }, + "Microsoft.Extensions.Options": { + "type": "CentralTransitive", + "requested": "[6.0.0, )", + "resolved": "6.0.0", + "contentHash": "dzXN0+V1AyjOe2xcJ86Qbo233KHuLEY0njf/P2Kw8SfJU+d45HNS2ctJdnEnrWbM9Ye2eFgaC5Mj9otRMU6IsQ==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "6.0.0", + "Microsoft.Extensions.Primitives": "6.0.0" + } + }, + "Microsoft.Toolkit.HighPerformance": { + "type": "CentralTransitive", + "requested": "[7.1.1, )", + "resolved": "7.1.1", + "contentHash": "TRnvDpZPXO30hTOtjfLw6Y9BtTKtTpzk9lefeh4RMCaUihWrVKQR454nYH4/mMJAh+LXqfAPyk0kfkJs0Amopw==" + }, + "NetFabric.Hyperlinq": { + "type": "CentralTransitive", + "requested": "[3.0.0-beta48, )", + "resolved": "3.0.0-beta48", + "contentHash": "oYUhXvxNS8bBJWqNkvx5g8y0P/0LtyqS2pN0w4OWjVDNWEpLbdbvPy9w/9z1n2PrqIjX3jxUsEnoCmxxGnI3gw==", + "dependencies": { + "NetFabric.Hyperlinq.Abstractions": "1.3.0", + "System.Buffers": "4.5.1", + "System.Runtime.CompilerServices.Unsafe": "5.0.0" + } + }, + "OrtKISharp": { + "type": "CentralTransitive", + "requested": "[0.0.2, )", + "resolved": "0.0.2", + "contentHash": "q8j0yR5836Zhv9WB9BFkQt1UaEFyibq8bqJcTiULlILF6/sz8z7Wy2N8sgYdDKsdW25zncIz7j6IDbKM5ynePg==", + "dependencies": { + "libortki": "0.0.2" + } + }, + "Singulink.Collections.Weak": { + "type": "CentralTransitive", + "requested": "[1.0.2, )", + "resolved": "1.0.2", + "contentHash": "giLAHrjJe0Bh7yhNexR6pmcv02+Fi+lEPxQVdB8zvkuJCmy6rnqu8CZLIpxrUfLcWDuTCSiK0IfGmMhig3UDhA==" + }, + "System.Reactive": { + "type": "CentralTransitive", + "requested": "[5.0.0, )", + "resolved": "5.0.0", + "contentHash": "erBZjkQHWL9jpasCE/0qKAryzVBJFxGHVBAvgRN1bzM0q2s1S4oYREEEL0Vb+1kA/6BKb5FjUZMp5VXmy+gzkQ==" + } + } + } +} \ No newline at end of file diff --git a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMFunctionBuilder.cs b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMFunctionBuilder.cs index bae39f9655..ec04643dbf 100644 --- a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMFunctionBuilder.cs +++ b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMFunctionBuilder.cs @@ -33,7 +33,7 @@ public StackVMFunctionBuilder(uint id, SectionManager sectionManager) _textEmitter = new StackVMEmitter(TextWriter); } - protected override ILinkableFunction CreateLinkableFunction(uint id, BaseFunction callable, IReadOnlyList functionRefs, byte[] text) + protected override ILinkableFunction CreateLinkableFunction(uint id, BaseFunction callable, IReadOnlyList functionRefs, Stream text) { return new StackVMLinkableFunction(id, callable, functionRefs, _localsAllocator.MaxCount, text, _context.CustomCallModules); } diff --git a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableFunction.cs b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableFunction.cs index 0ffa2203f4..64947f8c25 100644 --- a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableFunction.cs +++ b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableFunction.cs @@ -12,7 +12,7 @@ namespace Nncase.CodeGen.StackVM; internal class StackVMLinkableFunction : ILinkableFunction { - public StackVMLinkableFunction(uint id, BaseFunction sourceFunction, IEnumerable functionRefs, ushort maxLocals, byte[] text, IReadOnlySet custom_call_modules) + public StackVMLinkableFunction(uint id, BaseFunction sourceFunction, IEnumerable functionRefs, ushort maxLocals, Stream text, IReadOnlySet custom_call_modules) { Id = id; SourceFunction = sourceFunction; @@ -30,7 +30,7 @@ public StackVMLinkableFunction(uint id, BaseFunction sourceFunction, IEnumerable public ushort MaxLocals { get; } - public byte[] Text { get; } + public Stream Text { get; } public IReadOnlyList Sections => Array.Empty(); diff --git a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableModule.cs b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableModule.cs index 4ade1d2a8c..0ca1549f14 100644 --- a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableModule.cs +++ b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkableModule.cs @@ -30,7 +30,7 @@ public StackVMLinkableModule(IReadOnlyList functions, Section } } - protected override ILinkedModule CreateLinkedModule(IReadOnlyList linkedFunctions, byte[] text) + protected override ILinkedModule CreateLinkedModule(IReadOnlyList linkedFunctions, Stream text) { return new StackVMLinkedModule( linkedFunctions, diff --git a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkedModule.cs b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkedModule.cs index 06b554fb55..3bfb985fd8 100644 --- a/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkedModule.cs +++ b/modules/Nncase.Modules.StackVM/CodeGen/StackVM/StackVMLinkedModule.cs @@ -12,7 +12,7 @@ namespace Nncase.CodeGen.StackVM; internal class StackVMLinkedModule : ILinkedModule { - public StackVMLinkedModule(IReadOnlyList functions, byte[] text, byte[]? rdata, byte[]? custom_calls) + public StackVMLinkedModule(IReadOnlyList functions, Stream text, Stream? rdata, Stream? custom_calls) { Functions = functions; Sections = new[] diff --git a/src/Native/include/nncase/runtime/model.h b/src/Native/include/nncase/runtime/model.h index 257488360f..45c67c6430 100644 --- a/src/Native/include/nncase/runtime/model.h +++ b/src/Native/include/nncase/runtime/model.h @@ -48,29 +48,29 @@ struct model_header { struct function_header { uint32_t parameters; - uint32_t entrypoint; - uint32_t text_size; - uint32_t size; uint32_t sections; - uint32_t reserved0; + uint64_t entrypoint; + uint64_t text_size; + uint64_t size; }; struct module_header { module_kind_t kind; uint32_t version; - uint32_t size; uint32_t sections; uint32_t functions; + uint32_t reserved0; + uint64_t size; }; struct section_header { char name[MAX_SECTION_NAME_LENGTH]; uint32_t flags; - uint32_t size; - uint32_t body_start; - uint32_t body_size; - uint32_t memory_size; uint32_t reserved0; + uint64_t size; + uint64_t body_start; + uint64_t body_size; + uint64_t memory_size; }; NNCASE_INLINE_VAR constexpr uint32_t SECTION_MERGED_INTO_RDATA = 1; @@ -96,6 +96,6 @@ struct shape_header { }; NNCASE_INLINE_VAR constexpr uint32_t MODEL_IDENTIFIER = 'KMDL'; -NNCASE_INLINE_VAR constexpr uint32_t MODEL_VERSION = 6; +NNCASE_INLINE_VAR constexpr uint32_t MODEL_VERSION = 7; END_NS_NNCASE_RUNTIME diff --git a/src/Nncase.CodeGen/CodeGen/FunctionBuilder.cs b/src/Nncase.CodeGen/CodeGen/FunctionBuilder.cs index 9a374d3f11..27091514dd 100644 --- a/src/Nncase.CodeGen/CodeGen/FunctionBuilder.cs +++ b/src/Nncase.CodeGen/CodeGen/FunctionBuilder.cs @@ -47,7 +47,7 @@ public ILinkableFunction Build(BaseFunction callable) // 3. Fix addrs FixAddrs(); TextWriter.Flush(); - return CreateLinkableFunction(Id, callable, FunctionRefs, _textContent.ToArray()); + return CreateLinkableFunction(Id, callable, FunctionRefs, _textContent); } public void Dispose() @@ -59,7 +59,7 @@ public void Dispose() protected abstract void WriteText(); - protected abstract ILinkableFunction CreateLinkableFunction(uint id, BaseFunction callable, IReadOnlyList functionRefs, byte[] text); + protected abstract ILinkableFunction CreateLinkableFunction(uint id, BaseFunction callable, IReadOnlyList functionRefs, Stream text); private void FixAddrs() { diff --git a/src/Nncase.CodeGen/CodeGen/ILinkableFunction.cs b/src/Nncase.CodeGen/CodeGen/ILinkableFunction.cs index 39faa69578..21ce7e8a3b 100644 --- a/src/Nncase.CodeGen/CodeGen/ILinkableFunction.cs +++ b/src/Nncase.CodeGen/CodeGen/ILinkableFunction.cs @@ -18,7 +18,7 @@ public interface ILinkableFunction public IEnumerable FunctionRefs { get; } - public byte[] Text { get; } + public Stream Text { get; } public IReadOnlyList Sections { get; } } diff --git a/src/Nncase.CodeGen/CodeGen/KModelTypes.cs b/src/Nncase.CodeGen/CodeGen/KModelTypes.cs index 1a21449ceb..c384df87bc 100644 --- a/src/Nncase.CodeGen/CodeGen/KModelTypes.cs +++ b/src/Nncase.CodeGen/CodeGen/KModelTypes.cs @@ -30,11 +30,10 @@ public struct ModelHeader public struct FunctionHeader { public uint Parameters; - public uint Entrypoint; - public uint TextSize; - public uint Size; public uint Sections; - public uint Reserved0; + public ulong Entrypoint; + public ulong TextSize; + public ulong Size; } [StructLayout(LayoutKind.Sequential)] @@ -42,9 +41,10 @@ public unsafe struct ModuleHeader { public fixed byte Kind[ModelInfo.MaxModuleKindLength]; public uint Version; - public uint Size; public uint Sections; public uint Functions; + public uint Reserved0; + public ulong Size; } /// @@ -55,11 +55,11 @@ public unsafe struct SectionHeader { public fixed byte Name[ModelInfo.MaxSectionNameLength]; public uint Flags; - public uint Size; - public uint BodyStart; - public uint BodySize; - public uint MemorySize; public uint Reserved0; + public ulong Size; + public ulong BodyStart; + public ulong BodySize; + public ulong MemorySize; } [StructLayout(LayoutKind.Sequential)] diff --git a/src/Nncase.CodeGen/CodeGen/LinkableModule.cs b/src/Nncase.CodeGen/CodeGen/LinkableModule.cs index 7bdc85d600..0fdd3a54e1 100644 --- a/src/Nncase.CodeGen/CodeGen/LinkableModule.cs +++ b/src/Nncase.CodeGen/CodeGen/LinkableModule.cs @@ -32,21 +32,23 @@ public ILinkedModule Link(ILinkContext linkContext) foreach (var func in _functions) { FixFunctionRefs(func, linkContext); + bw.Flush(); bw.AlignPosition(_textAlignment); var textBegin = bw.Position(); - bw.Write(func.Text); - linkedFunctions.Add(new LinkedFunction(func.Id, func.SourceFunction, (uint)textBegin, (uint)func.Text.Length, func.Sections)); + func.Text.Position = 0; + func.Text.CopyTo(bw.BaseStream); + linkedFunctions.Add(new LinkedFunction(func.Id, func.SourceFunction, (ulong)textBegin, (ulong)func.Text.Length, func.Sections)); } } - return CreateLinkedModule(linkedFunctions, text.ToArray()); + return CreateLinkedModule(linkedFunctions, text); } - protected abstract ILinkedModule CreateLinkedModule(IReadOnlyList linkedFunctions, byte[] text); + protected abstract ILinkedModule CreateLinkedModule(IReadOnlyList linkedFunctions, Stream text); private void FixFunctionRefs(ILinkableFunction func, ILinkContext linkContext) { - using var writer = new BinaryWriter(new MemoryStream(func.Text)); + using var writer = new BinaryWriter(func.Text, Encoding.UTF8, leaveOpen: true); foreach (var funcRef in func.FunctionRefs) { var id = linkContext.GetFunctionId(funcRef.Callable); diff --git a/src/Nncase.CodeGen/CodeGen/LinkedFunction.cs b/src/Nncase.CodeGen/CodeGen/LinkedFunction.cs index df9ff51a28..b3dbe692e0 100644 --- a/src/Nncase.CodeGen/CodeGen/LinkedFunction.cs +++ b/src/Nncase.CodeGen/CodeGen/LinkedFunction.cs @@ -12,7 +12,7 @@ namespace Nncase.CodeGen; public class LinkedFunction : ILinkedFunction { - public LinkedFunction(uint id, Callable sourceFunction, uint textBegin, uint textLength, IReadOnlyList sections) + public LinkedFunction(uint id, Callable sourceFunction, ulong textBegin, ulong textLength, IReadOnlyList sections) { Id = id; CompilerServices.InferenceType(sourceFunction); @@ -29,9 +29,9 @@ public LinkedFunction(uint id, Callable sourceFunction, uint textBegin, uint tex public IRType ReturnType { get; } - public uint TextBegin { get; } + public ulong TextBegin { get; } - public uint TextLength { get; } + public ulong TextLength { get; } public IReadOnlyList Sections { get; } } diff --git a/src/Nncase.CodeGen/CodeGen/LinkedModel.cs b/src/Nncase.CodeGen/CodeGen/LinkedModel.cs index a7a25a65e2..72461aaf69 100644 --- a/src/Nncase.CodeGen/CodeGen/LinkedModel.cs +++ b/src/Nncase.CodeGen/CodeGen/LinkedModel.cs @@ -103,7 +103,7 @@ private unsafe void Serialize(BinaryWriter writer, ILinkedModule module) var endPos = writer.Position(); // Write header - header.Size = (uint)(endPos - headerPos); + header.Size = (ulong)(endPos - headerPos); writer.Position(headerPos); writer.Write(ref header); writer.Position(endPos); @@ -138,7 +138,7 @@ private unsafe void Serialize(BinaryWriter writer, ILinkedFunction func) var endPos = writer.Position(); // Write header - funcHeader.Size = (uint)(endPos - headerPos); + funcHeader.Size = (ulong)(endPos - headerPos); writer.Position(headerPos); writer.Write(ref funcHeader); writer.Position(endPos); @@ -157,7 +157,7 @@ private unsafe void Serialize(BinaryWriter writer, ILinkedSection section) var headerPos = writer.Position(); writer.Skip((ulong)sizeof(SectionHeader)); - header.BodyStart = (uint)writer.AlignPosition(section.Alignment); + header.BodyStart = (ulong)writer.AlignPosition(section.Alignment); writer.Flush(); section.Serialize(writer.BaseStream); @@ -165,7 +165,7 @@ private unsafe void Serialize(BinaryWriter writer, ILinkedSection section) var endPos = writer.Position(); // Write header - header.Size = (uint)(endPos - headerPos); + header.Size = (ulong)(endPos - headerPos); writer.Position(headerPos); writer.Write(ref header); writer.Position(endPos); diff --git a/src/Nncase.CodeGen/CodeGen/LinkedSection.cs b/src/Nncase.CodeGen/CodeGen/LinkedSection.cs index 6e1c670bcc..2bbddefd90 100644 --- a/src/Nncase.CodeGen/CodeGen/LinkedSection.cs +++ b/src/Nncase.CodeGen/CodeGen/LinkedSection.cs @@ -11,16 +11,16 @@ namespace Nncase.CodeGen; public class LinkedSection : ILinkedSection { - private readonly byte[]? _content; + private readonly Stream? _content; - public LinkedSection(byte[]? content, string name, uint flags, uint alignment, uint sizeInMemory) + public LinkedSection(Stream? content, string name, uint flags, uint alignment, ulong sizeInMemory) { if (alignment == 0) { throw new ArgumentOutOfRangeException(nameof(alignment)); } - SizeInFile = (uint?)content?.Length ?? 0; + SizeInFile = (ulong?)content?.Length ?? 0; if (sizeInMemory < SizeInFile) { throw new ArgumentOutOfRangeException(nameof(sizeInMemory)); @@ -39,15 +39,16 @@ public LinkedSection(byte[]? content, string name, uint flags, uint alignment, u public uint Alignment { get; } - public uint SizeInFile { get; } + public ulong SizeInFile { get; } - public uint SizeInMemory { get; } + public ulong SizeInMemory { get; } public void Serialize(Stream output) { if (_content != null) { - output.Write(_content); + _content.Seek(0, SeekOrigin.Begin); + _content.CopyTo(output); } } } diff --git a/src/Nncase.CodeGen/CodeGen/SectionManager.cs b/src/Nncase.CodeGen/CodeGen/SectionManager.cs index c37e368fde..cc18c0e36b 100644 --- a/src/Nncase.CodeGen/CodeGen/SectionManager.cs +++ b/src/Nncase.CodeGen/CodeGen/SectionManager.cs @@ -45,26 +45,31 @@ public record FunctionRef(long Position, int Length, BaseFunction Callable, Func public class SectionManager { - private readonly Dictionary _sections = new(); + private readonly Dictionary _sections = new(); public BinaryWriter GetWriter(string name) { if (!_sections.TryGetValue(name, out var section)) { - var stream = new MemoryStream(); - section = (stream, new BinaryWriter(stream, Encoding.UTF8, true)); + var tmpFile = File.Open(Path.GetTempFileName(), new FileStreamOptions + { + Access = FileAccess.ReadWrite, + Mode = FileMode.Create, + Options = FileOptions.Asynchronous | FileOptions.DeleteOnClose, + }); + section = (tmpFile, new BinaryWriter(tmpFile, Encoding.UTF8, true)); _sections.Add(name, section); } return section.Writer; } - public byte[]? GetContent(string name) + public Stream? GetContent(string name) { if (_sections.TryGetValue(name, out var section)) { section.Writer.Flush(); - return section.Stream.ToArray(); + return section.Stream; } return null; diff --git a/src/Nncase.CodeGen/CodeGenExtensions.cs b/src/Nncase.CodeGen/CodeGenExtensions.cs index 7eee489db6..b73a9d2573 100644 --- a/src/Nncase.CodeGen/CodeGenExtensions.cs +++ b/src/Nncase.CodeGen/CodeGenExtensions.cs @@ -54,7 +54,14 @@ public static long Position(this BinaryWriter writer) /// The current position. public static long Position(this BinaryWriter writer, long pos) { - return writer.Seek((int)pos, SeekOrigin.Begin); + writer.Seek(0, SeekOrigin.Begin); + var segs = Math.DivRem(pos, int.MaxValue, out var rem); + for (long l = 0; l < segs; l++) + { + writer.Seek(int.MaxValue, SeekOrigin.Current); + } + + return writer.Seek(checked((int)rem), SeekOrigin.Current); } /// @@ -64,7 +71,7 @@ public static long Position(this BinaryWriter writer, long pos) /// Bytes to skip. public static void Skip(this BinaryWriter writer, ulong len) { - writer.Seek((int)len, SeekOrigin.Current); + writer.Seek(checked((int)len), SeekOrigin.Current); } public static unsafe void Write(this BinaryWriter writer, ref T value) diff --git a/src/Nncase.Core/CodeGen/CodeGenTypes.cs b/src/Nncase.Core/CodeGen/CodeGenTypes.cs index f39fdb456f..f44bcebacd 100644 --- a/src/Nncase.Core/CodeGen/CodeGenTypes.cs +++ b/src/Nncase.Core/CodeGen/CodeGenTypes.cs @@ -47,7 +47,7 @@ public static class ModelInfo /// /// kmodel version. /// - public const int Version = 6; + public const int Version = 7; /// /// merged rdata flag. diff --git a/src/Nncase.Core/CodeGen/IModuleBuilder.cs b/src/Nncase.Core/CodeGen/IModuleBuilder.cs index fc31f69235..bb9a520553 100644 --- a/src/Nncase.Core/CodeGen/IModuleBuilder.cs +++ b/src/Nncase.Core/CodeGen/IModuleBuilder.cs @@ -46,12 +46,12 @@ public interface ILinkedSection /// /// Gets size in file. /// - uint SizeInFile { get; } + ulong SizeInFile { get; } /// /// Gets size in memory. /// - uint SizeInMemory { get; } + ulong SizeInMemory { get; } /// /// Serialize payload. @@ -83,12 +83,12 @@ public interface ILinkedFunction /// /// Gets text begin. /// - uint TextBegin { get; } + ulong TextBegin { get; } /// /// Gets text length. /// - uint TextLength { get; } + ulong TextLength { get; } /// /// Gets sections. diff --git a/src/Nncase.Tests.TestFixture/packages.lock.json b/src/Nncase.Tests.TestFixture/packages.lock.json index a42ac54c45..13a2baae60 100644 --- a/src/Nncase.Tests.TestFixture/packages.lock.json +++ b/src/Nncase.Tests.TestFixture/packages.lock.json @@ -58,9 +58,9 @@ }, "xunit.runner.visualstudio": { "type": "Direct", - "requested": "[2.4.3, )", - "resolved": "2.4.3", - "contentHash": "kZZSmOmKA8OBlAJaquPXnJJLM9RwQ27H7BMVqfMLUcTi9xHinWGJiWksa3D4NEtz0wZ/nxd2mogObvBgJKCRhQ==" + "requested": "[2.5.0, )", + "resolved": "2.5.0", + "contentHash": "+Gp9vuC2431yPyKB15YrOTxCuEAErBQUTIs6CquumX1F073UaPHGW0VE/XVJLMh9W4sXdz3TBkcHdFWZrRn2Hw==" }, "xunit.v3.assert": { "type": "Direct", diff --git a/src/Nncase.Tests/Core/UnitTestTIR.cs b/src/Nncase.Tests/Core/UnitTestTIR.cs index ba7131f64e..54d504f58c 100644 --- a/src/Nncase.Tests/Core/UnitTestTIR.cs +++ b/src/Nncase.Tests/Core/UnitTestTIR.cs @@ -82,7 +82,7 @@ public void TestSizeVar() var name = "test"; var actual = T.SizeVar(name); var expected = Var.SizeVar(name); - Assert.True(actual.ToString().Equals(expected.ToString(), StringComparison.Ordinal)); + Assert.Equal(expected.ToString(), actual.ToString()); } [Fact] diff --git a/src/Nncase.Tests/packages.lock.json b/src/Nncase.Tests/packages.lock.json index b08aa2c881..dce3ce0116 100644 --- a/src/Nncase.Tests/packages.lock.json +++ b/src/Nncase.Tests/packages.lock.json @@ -108,9 +108,9 @@ }, "xunit.analyzers": { "type": "Direct", - "requested": "[1.1.0, )", - "resolved": "1.1.0", - "contentHash": "pFgSns6v+apWOqKaMpbi7LG7iHy9Dmhz54ySJKnn2f8jsArRsnOXf0Naqxy2+g0Q8YI7xhkfxT6YVoY+4GcQFg==" + "requested": "[1.2.0, )", + "resolved": "1.2.0", + "contentHash": "d3dehV/DASLRlR8stWQmbPPjfYC2tct50Evav+OlsJMkfFqkhYvzO1k0s81lk0px8O0knZU/FqC8SqbXOtn+hw==" }, "Xunit.Combinatorial": { "type": "Direct", @@ -123,12 +123,12 @@ }, "xunit.core": { "type": "Direct", - "requested": "[2.4.2, )", - "resolved": "2.4.2", - "contentHash": "KB4yGCxNqIVyekhJLXtKSEq6BaXVp/JO3mbGVE1hxypZTLEe7h+sTbAhpA+yZW2dPtXTuiW+C1B2oxxHEkrmOw==", + "requested": "[2.5.0, )", + "resolved": "2.5.0", + "contentHash": "dnV0Mn2s1C0y2m33AylQyMkEyhBQsL4R0302kwSGiEGuY3JwzEmhTa9pnghyMRPliYSs4fXfkEAP+5bKXryGFg==", "dependencies": { - "xunit.extensibility.core": "[2.4.2]", - "xunit.extensibility.execution": "[2.4.2]" + "xunit.extensibility.core": "[2.5.0]", + "xunit.extensibility.execution": "[2.5.0]" } }, "Xunit.DependencyInjection": { @@ -145,9 +145,9 @@ }, "xunit.runner.visualstudio": { "type": "Direct", - "requested": "[2.4.3, )", - "resolved": "2.4.3", - "contentHash": "kZZSmOmKA8OBlAJaquPXnJJLM9RwQ27H7BMVqfMLUcTi9xHinWGJiWksa3D4NEtz0wZ/nxd2mogObvBgJKCRhQ==" + "requested": "[2.5.0, )", + "resolved": "2.5.0", + "contentHash": "+Gp9vuC2431yPyKB15YrOTxCuEAErBQUTIs6CquumX1F073UaPHGW0VE/XVJLMh9W4sXdz3TBkcHdFWZrRn2Hw==" }, "xunit.v3.assert": { "type": "Direct", @@ -1439,8 +1439,8 @@ }, "xunit.extensibility.core": { "type": "Transitive", - "resolved": "2.4.2", - "contentHash": "W1BoXTIN1C6kpVSMw25huSet25ky6IAQUNovu3zGOGN/jWnbgSoTyCrlIhmXSg0tH5nEf8q7h3OjNHOjyu5PfA==", + "resolved": "2.5.0", + "contentHash": "xRm6NIV3i7I+LkjsAJ91Xz2fxJm/oMEi2CYq1G5HlGTgcK1Zo2wNbLO6nKX1VG5FZzXibSdoLwr/MofVvh3mFA==", "dependencies": { "NETStandard.Library": "1.6.1", "xunit.abstractions": "2.0.3" @@ -1448,11 +1448,11 @@ }, "xunit.extensibility.execution": { "type": "Transitive", - "resolved": "2.4.2", - "contentHash": "CZmgcKkwpyo8FlupZdWpJCryrAOWLh1FBPG6gmVZuPQkGQsim/oL4PcP4nfrC2hHgXUFtluvaJ0Sp9PQKUMNpg==", + "resolved": "2.5.0", + "contentHash": "7+v2Bvp+1ew1iMGQVb1glICi8jcNdHbRUX6Ru0dmJBViGdjiS7kyqcX2VxleQhFbKNi+WF0an7/TeTXD283RlQ==", "dependencies": { "NETStandard.Library": "1.6.1", - "xunit.extensibility.core": "[2.4.2]" + "xunit.extensibility.core": "[2.5.0]" } }, "nncase.codegen": {