├── .idea └── .idea.GGMLSharp │ └── .idea │ ├── .name │ ├── encodings.xml │ ├── vcs.xml │ ├── indexLayout.xml │ └── .gitignore ├── README.md ├── GGMLSharp ├── GGMLSharp.csproj └── TypeDefinitions.cs ├── Test0 ├── Test0.csproj └── Program.cs ├── Test2 ├── Test2.csproj └── Program.cs ├── Test3 ├── Test3.csproj └── Program.cs ├── LICENSE.txt ├── Test1 ├── Test1.csproj └── Program.cs ├── .gitattributes ├── GGMLSharp.sln └── .gitignore /.idea/.idea.GGMLSharp/.idea/.name: -------------------------------------------------------------------------------- 1 | GGMLSharp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GGML# 2 | 3 | Port of [GGML](https://github.com/ggerganov/ggml) to C#. 4 | 5 | Yeah, I know, it seems to be C# community is just trying to catch wind as always. But why not. 6 | -------------------------------------------------------------------------------- /.idea/.idea.GGMLSharp/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/.idea.GGMLSharp/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/.idea.GGMLSharp/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.GGMLSharp/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Rider ignored files 5 | /modules.xml 6 | /projectSettingsUpdater.xml 7 | /contentModel.xml 8 | /.idea.GGMLSharp.iml 9 | # Editor-based HTTP Client requests 10 | /httpRequests/ 11 | # Datasource local storage ignored files 12 | /dataSources/ 13 | /dataSources.local.xml 14 | -------------------------------------------------------------------------------- /GGMLSharp/GGMLSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | true 8 | preview 9 | $(DefineConstants);GGML_GELU_FP16;GGML_SILU_FP16 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Test0/Test0.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | preview 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Test2/Test2.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | preview 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Test3/Test3.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | preview 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Test1/Test1.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | preview 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\editbin.exe 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Test0/Program.cs: -------------------------------------------------------------------------------- 1 | using GGMLSharp; 2 | using System.Diagnostics; 3 | using static GGMLSharp.Ggml; 4 | 5 | //#undef INIT_TABLES 6 | 7 | unsafe 8 | { 9 | ggml_init_params init_params = default; 10 | { 11 | init_params.mem_size = 128 * 1024 * 1024; 12 | init_params.mem_buffer = null; 13 | init_params.no_alloc = false; 14 | }; 15 | 16 | ggml_context* ctx0 = ggml_init(init_params); 17 | 18 | ggml_tensor* t1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 10); 19 | ggml_tensor* t2 = ggml_new_tensor_2d(ctx0, ggml_type.GGML_TYPE_I16, 10, 20); 20 | ggml_tensor* t3 = ggml_new_tensor_3d(ctx0, ggml_type.GGML_TYPE_I32, 10, 20, 30); 21 | 22 | Debug.Assert(t1->n_dims == 1); 23 | Debug.Assert(t1->ne[0] == 10); 24 | Debug.Assert(t1->nb[1] == 10 * sizeof(float)); 25 | 26 | Debug.Assert(t2->n_dims == 2); 27 | Debug.Assert(t2->ne[0] == 10); 28 | Debug.Assert(t2->ne[1] == 20); 29 | Debug.Assert(t2->nb[1] == 10 * sizeof(Int16)); 30 | Debug.Assert(t2->nb[2] == 10 * 20 * sizeof(Int16)); 31 | 32 | Debug.Assert(t3->n_dims == 3); 33 | Debug.Assert(t3->ne[0] == 10); 34 | Debug.Assert(t3->ne[1] == 20); 35 | Debug.Assert(t3->ne[2] == 30); 36 | Debug.Assert(t3->nb[1] == 10 * sizeof(Int32)); 37 | Debug.Assert(t3->nb[2] == 10 * 20 * sizeof(Int32)); 38 | Debug.Assert(t3->nb[3] == 10 * 20 * 30 * sizeof(Int32)); 39 | 40 | ggml_print_objects(ctx0); 41 | 42 | ggml_free(ctx0); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /GGMLSharp.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.7.33711.374 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test0", "Test0\Test0.csproj", "{18621E7B-8FA7-4AF3-B009-61AFCB87981E}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GGMLSharp", "GGMLSharp\GGMLSharp.csproj", "{E4125078-7724-435B-8062-972E54E659D4}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test1", "Test1\Test1.csproj", "{5D62E400-BE6B-415D-AC06-50348D130968}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{57A5E9DF-62C9-48F4-973B-AAC0070221DA}" 13 | ProjectSection(SolutionItems) = preProject 14 | README.md = README.md 15 | EndProjectSection 16 | EndProject 17 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test2", "Test2\Test2.csproj", "{A9649CE7-891D-490D-A044-741D407B990E}" 18 | EndProject 19 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test3", "Test3\Test3.csproj", "{3CA61F44-92DE-4391-85D3-8DF3D57FD712}" 20 | EndProject 21 | Global 22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 23 | Debug|Any CPU = Debug|Any CPU 24 | Release|Any CPU = Release|Any CPU 25 | EndGlobalSection 26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 27 | {18621E7B-8FA7-4AF3-B009-61AFCB87981E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {18621E7B-8FA7-4AF3-B009-61AFCB87981E}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {18621E7B-8FA7-4AF3-B009-61AFCB87981E}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {18621E7B-8FA7-4AF3-B009-61AFCB87981E}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {E4125078-7724-435B-8062-972E54E659D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {E4125078-7724-435B-8062-972E54E659D4}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {E4125078-7724-435B-8062-972E54E659D4}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {E4125078-7724-435B-8062-972E54E659D4}.Release|Any CPU.Build.0 = Release|Any CPU 35 | {5D62E400-BE6B-415D-AC06-50348D130968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {5D62E400-BE6B-415D-AC06-50348D130968}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {5D62E400-BE6B-415D-AC06-50348D130968}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {5D62E400-BE6B-415D-AC06-50348D130968}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {A9649CE7-891D-490D-A044-741D407B990E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {A9649CE7-891D-490D-A044-741D407B990E}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {A9649CE7-891D-490D-A044-741D407B990E}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {A9649CE7-891D-490D-A044-741D407B990E}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {3CA61F44-92DE-4391-85D3-8DF3D57FD712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {3CA61F44-92DE-4391-85D3-8DF3D57FD712}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {3CA61F44-92DE-4391-85D3-8DF3D57FD712}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {3CA61F44-92DE-4391-85D3-8DF3D57FD712}.Release|Any CPU.Build.0 = Release|Any CPU 47 | EndGlobalSection 48 | GlobalSection(SolutionProperties) = preSolution 49 | HideSolutionNode = FALSE 50 | EndGlobalSection 51 | GlobalSection(ExtensibilityGlobals) = postSolution 52 | SolutionGuid = {CC30738A-F474-45AB-8686-75895DB5F46B} 53 | EndGlobalSection 54 | EndGlobal 55 | -------------------------------------------------------------------------------- /Test3/Program.cs: -------------------------------------------------------------------------------- 1 | using GGMLSharp; 2 | using System.Diagnostics; 3 | using static GGMLSharp.Ggml; 4 | 5 | ulong next = 1; 6 | int RAND_MAX = 32767; 7 | 8 | unsafe 9 | { 10 | ggml_init_params init_params = default; 11 | { 12 | init_params.mem_size = 1024 * 1024 * 1024; 13 | init_params.mem_buffer = null; 14 | init_params.no_alloc = false; 15 | }; 16 | 17 | ggml_opt_params opt_params = ggml_opt_default_params(ggml_opt_type.GGML_OPT_LBFGS); 18 | //ggml_opt_params opt_params = ggml_opt_default_params(ggml_opt_type.GGML_OPT_ADAM); 19 | 20 | opt_params.n_threads = (args.Length > 0) ? int.Parse(args[1]) : 8; 21 | 22 | const int NP = 1 << 12; 23 | const int NF = 1 << 8; 24 | 25 | ggml_context * ctx0 = ggml_init(init_params); 26 | 27 | ggml_tensor * F = ggml_new_tensor_2d(ctx0, ggml_type.GGML_TYPE_F32, NF, NP); 28 | ggml_tensor * l = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, NP); 29 | 30 | // regularization weight 31 | ggml_tensor * lambda = ggml_new_f32(ctx0, 1e-5f); 32 | 33 | xsrand(0); 34 | 35 | for (int j = 0; j < NP; j++) { 36 | float ll = j < NP/2 ? 1.0f : -1.0f; 37 | ((float *)l->data)[j] = ll; 38 | 39 | for (int i = 0; i < NF; i++) { 40 | ((float *)F->data)[j*NF + i] = ((ll > 0 && i < NF/2 ? 1.0f : ll < 0 && i >= NF/2 ? 1.0f : 0.0f) + ((float)xrand()/(float)RAND_MAX - 0.5f)*0.1f)/(0.5f*NF); 41 | } 42 | } 43 | 44 | { 45 | // initial guess 46 | ggml_tensor * x = ggml_set_f32(ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, NF), 0.0f); 47 | 48 | ggml_set_param(ctx0, x); 49 | 50 | // f = sum_j[(f_j*x - l)^2]/n + lambda*|x^2| 51 | ggml_tensor* f = 52 | ggml_add(ctx0, 53 | ggml_div(ctx0, 54 | ggml_sum(ctx0, 55 | ggml_sqr(ctx0, 56 | ggml_sub(ctx0, 57 | ggml_mul_mat(ctx0, F, x), 58 | l) 59 | ) 60 | ), 61 | ggml_new_f32(ctx0, NP) 62 | ), 63 | ggml_mul(ctx0, 64 | ggml_sum(ctx0, ggml_sqr(ctx0, x)), 65 | lambda) 66 | ); 67 | 68 | ggml_opt_result res = ggml_opt(null, opt_params, f); 69 | 70 | Debug.Assert(res == ggml_opt_result.GGML_OPT_OK); 71 | 72 | // print results 73 | for (int i = 0; i < 16; i++) { 74 | Console.WriteLine($"x[{i,3}] = {0:F6}", ((float *)x->data)[i]); 75 | } 76 | Console.WriteLine("..."); 77 | for (int i = NF - 16; i < NF; i++) { 78 | Console.WriteLine($"x[{i,3}] = {0:F6}", ((float *)x->data)[i]); 79 | } 80 | Console.WriteLine(); 81 | 82 | for (int i = 0; i < NF; ++i) { 83 | if (i < NF/2) { 84 | Debug.Assert(is_close(((float *)x->data)[i], 1.0f, 1e-2f)); 85 | } else { 86 | Debug.Assert(is_close(((float *)x->data)[i], -1.0f, 1e-2f)); 87 | } 88 | } 89 | } 90 | 91 | ggml_free(ctx0); 92 | } 93 | 94 | static bool is_close(float a, float b, float epsilon) { 95 | return Math.Abs(a - b) < epsilon; 96 | } 97 | 98 | int xrand() // RAND_MAX assumed to be 32767 99 | { 100 | next = next * 214013L + 2531011; 101 | return (int)((next >> 16) & 0x7FFF); 102 | } 103 | 104 | void xsrand(uint seed) 105 | { 106 | next = seed; 107 | } -------------------------------------------------------------------------------- /Test2/Program.cs: -------------------------------------------------------------------------------- 1 | using GGMLSharp; 2 | using System.Diagnostics; 3 | using static GGMLSharp.Ggml; 4 | 5 | unsafe 6 | { 7 | ggml_init_params init_params = default; 8 | { 9 | init_params.mem_size = 128 * 1024 * 1024; 10 | init_params.mem_buffer = null; 11 | init_params.no_alloc = false; 12 | }; 13 | 14 | //ggml_opt_params opt_params = ggml_opt_default_params(ggml_opt_type.GGML_OPT_LBFGS); 15 | 16 | ggml_opt_params opt_params = ggml_opt_default_params(ggml_opt_type.GGML_OPT_ADAM); 17 | opt_params.adam.alpha = 0.01f; 18 | 19 | // original threads: 8 20 | int nthreads = 8; 21 | string env = Environment.GetEnvironmentVariable("GGML_NTHREADS"); 22 | if (!string.IsNullOrWhiteSpace(env)) { 23 | nthreads = int.Parse(env); 24 | } 25 | if (args.Length > 1) { 26 | nthreads = int.Parse(args[0]); 27 | } 28 | opt_params.n_threads = nthreads; 29 | Console.WriteLine($"test2: n_threads:{opt_params.n_threads}"); 30 | 31 | float[] xi = new []{ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f , 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, }; 32 | float[] yi = new []{ 15.0f, 25.0f, 35.0f, 45.0f, 55.0f, 65.0f, 75.0f, 85.0f, 95.0f, 105.0f, }; 33 | 34 | int n = xi.Length; 35 | 36 | ggml_context * ctx0 = ggml_init(init_params); 37 | 38 | ggml_tensor * x = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, n); 39 | ggml_tensor * y = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, n); 40 | 41 | for (int i = 0; i < n; i++) { 42 | ((float *) x->data)[i] = xi[i]; 43 | ((float *) y->data)[i] = yi[i]; 44 | } 45 | 46 | { 47 | ggml_tensor * t0 = ggml_new_f32(ctx0, 0.0f); 48 | ggml_tensor * t1 = ggml_new_f32(ctx0, 0.0f); 49 | 50 | // initialize auto-diff parameters: 51 | ggml_set_param(ctx0, t0); 52 | ggml_set_param(ctx0, t1); 53 | 54 | // f = sum_i[(t0 + t1*x_i - y_i)^2]/(2n) 55 | ggml_tensor * f = 56 | ggml_div(ctx0, 57 | ggml_sum(ctx0, 58 | ggml_sqr(ctx0, 59 | ggml_sub(ctx0, 60 | ggml_add(ctx0, 61 | ggml_mul(ctx0, x, ggml_repeat(ctx0, t1, x)), 62 | ggml_repeat(ctx0, t0, x)), 63 | y) 64 | ) 65 | ), 66 | ggml_new_f32(ctx0, 2.0f*n)); 67 | 68 | ggml_opt_result res = ggml_opt(null, opt_params, f); 69 | 70 | Debug.Assert(res == ggml_opt_result.GGML_OPT_OK); 71 | 72 | Console.WriteLine("t0 = {0:F6}", ggml_get_f32_1d(t0, 0)); 73 | Console.WriteLine("t1 = {0:F6}", ggml_get_f32_1d(t1, 0)); 74 | 75 | Debug.Assert(is_close(ggml_get_f32_1d(t0, 0), 5.0f, 1e-3f)); 76 | Debug.Assert(is_close(ggml_get_f32_1d(t1, 0), 10.0f, 1e-3f)); 77 | } 78 | 79 | { 80 | ggml_tensor * t0 = ggml_new_f32(ctx0, -1.0f); 81 | ggml_tensor * t1 = ggml_new_f32(ctx0, 9.0f); 82 | 83 | ggml_set_param(ctx0, t0); 84 | ggml_set_param(ctx0, t1); 85 | 86 | // f = 0.5*sum_i[abs(t0 + t1*x_i - y_i)]/n 87 | ggml_tensor * f = 88 | ggml_mul(ctx0, 89 | ggml_new_f32(ctx0, 1.0f/(2*n)), 90 | ggml_sum(ctx0, 91 | ggml_abs(ctx0, 92 | ggml_sub(ctx0, 93 | ggml_add(ctx0, 94 | ggml_mul(ctx0, x, ggml_repeat(ctx0, t1, x)), 95 | ggml_repeat(ctx0, t0, x)), 96 | y) 97 | ) 98 | ) 99 | ); 100 | 101 | 102 | ggml_opt_result res = ggml_opt(null, opt_params, f); 103 | 104 | Debug.Assert(res == ggml_opt_result.GGML_OPT_OK); 105 | Debug.Assert(is_close(ggml_get_f32_1d(t0, 0), 5.0f, 1e-2f)); 106 | Debug.Assert(is_close(ggml_get_f32_1d(t1, 0), 10.0f, 1e-2f)); 107 | } 108 | 109 | { 110 | ggml_tensor * t0 = ggml_new_f32(ctx0, 5.0f); 111 | ggml_tensor * t1 = ggml_new_f32(ctx0, -4.0f); 112 | 113 | ggml_set_param(ctx0, t0); 114 | ggml_set_param(ctx0, t1); 115 | 116 | // f = t0^2 + t1^2 117 | ggml_tensor * f = 118 | ggml_add(ctx0, 119 | ggml_sqr(ctx0, t0), 120 | ggml_sqr(ctx0, t1) 121 | ); 122 | 123 | ggml_opt_result res = ggml_opt(null, opt_params, f); 124 | 125 | Debug.Assert(res == ggml_opt_result.GGML_OPT_OK); 126 | Debug.Assert(is_close(ggml_get_f32_1d(f, 0), 0.0f, 1e-3f)); 127 | Debug.Assert(is_close(ggml_get_f32_1d(t0, 0), 0.0f, 1e-3f)); 128 | Debug.Assert(is_close(ggml_get_f32_1d(t1, 0), 0.0f, 1e-3f)); 129 | } 130 | 131 | ///////////////////////////////////////// 132 | 133 | { 134 | ggml_tensor * t0 = ggml_new_f32(ctx0, -7.0f); 135 | ggml_tensor * t1 = ggml_new_f32(ctx0, 8.0f); 136 | 137 | ggml_set_param(ctx0, t0); 138 | ggml_set_param(ctx0, t1); 139 | 140 | // f = (t0 + 2*t1 - 7)^2 + (2*t0 + t1 - 5)^2 141 | ggml_tensor * f = 142 | ggml_add(ctx0, 143 | ggml_sqr(ctx0, 144 | ggml_sub(ctx0, 145 | ggml_add(ctx0, 146 | t0, 147 | ggml_mul(ctx0, t1, ggml_new_f32(ctx0, 2.0f))), 148 | ggml_new_f32(ctx0, 7.0f) 149 | ) 150 | ), 151 | ggml_sqr(ctx0, 152 | ggml_sub(ctx0, 153 | ggml_add(ctx0, 154 | ggml_mul(ctx0, t0, ggml_new_f32(ctx0, 2.0f)), 155 | t1), 156 | ggml_new_f32(ctx0, 5.0f) 157 | ) 158 | ) 159 | ); 160 | 161 | ggml_opt_result res = ggml_opt(null, opt_params, f); 162 | 163 | Debug.Assert(res == ggml_opt_result.GGML_OPT_OK); 164 | Debug.Assert(is_close(ggml_get_f32_1d(f, 0), 0.0f, 1e-3f)); 165 | Debug.Assert(is_close(ggml_get_f32_1d(t0, 0), 1.0f, 1e-3f)); 166 | Debug.Assert(is_close(ggml_get_f32_1d(t1, 0), 3.0f, 1e-3f)); 167 | } 168 | 169 | ggml_free(ctx0); 170 | } 171 | 172 | static bool is_close(float a, float b, float epsilon) { 173 | return Math.Abs(a - b) < epsilon; 174 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /GGMLSharp/TypeDefinitions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace GGMLSharp; 5 | 6 | [InlineArray(64 /*GGML_MAX_CONTEXTS*/)] 7 | public struct Buffer64 8 | { 9 | private T _element0; 10 | 11 | [UnscopedRef] 12 | public ref T this[int i] => ref Unsafe.Add(ref _element0, i); 13 | } 14 | 15 | [InlineArray(4096 /*GGML_MAX_NODES*/)] 16 | public struct Buffer4096 where T : unmanaged 17 | { 18 | private T _element0; 19 | 20 | [UnscopedRef] 21 | public ref T this[int i] => ref Unsafe.Add(ref _element0, i); 22 | } 23 | 24 | public unsafe struct ggml_init_params 25 | { 26 | // memory pool 27 | public ulong mem_size; // bytes 28 | public void* mem_buffer; // if NULL, memory will be allocated internally 29 | public bool no_alloc; // don't allocate memory for the tensor data 30 | } 31 | 32 | public unsafe struct ggml_context 33 | { 34 | public ulong mem_size; 35 | public void* mem_buffer; 36 | public bool mem_buffer_owned; 37 | public bool no_alloc; 38 | 39 | public int n_objects; 40 | 41 | public ggml_object* objects_begin; 42 | public ggml_object* objects_end; 43 | 44 | public ggml_scratch scratch; 45 | public ggml_scratch scratch_save; 46 | } 47 | 48 | public unsafe struct ggml_object 49 | { 50 | public ulong offs; 51 | public ulong size; 52 | 53 | public ggml_object* next; 54 | 55 | public fixed byte padding[8]; 56 | } 57 | 58 | public unsafe struct ggml_scratch 59 | { 60 | public ulong offs; 61 | public ulong size; 62 | public void* data; 63 | } 64 | 65 | public unsafe struct ggml_tensor 66 | { 67 | const int GGML_MAX_DIMS = 4; 68 | const int GGML_MAX_OPT = 4; 69 | public ggml_type type; 70 | 71 | public int n_dims; 72 | public fixed long ne[GGML_MAX_DIMS]; // number of elements 73 | public fixed ulong nb[GGML_MAX_DIMS]; // stride in bytes: 74 | // nb[0] = sizeof(type) 75 | // nb[1] = nb[0] * ne[0] + padding 76 | // nb[i] = nb[i-1] * ne[i-1] 77 | 78 | // compute data 79 | public ggml_op op; 80 | 81 | public bool is_param; 82 | 83 | public ggml_tensor* grad; 84 | public ggml_tensor* src0; 85 | public ggml_tensor* src1; 86 | //public fixed ggml_tensor* opt[GGML_MAX_OPT]; 87 | public fixed long opt[GGML_MAX_OPT]; 88 | 89 | // thread scheduling 90 | public int n_tasks; 91 | 92 | // performance 93 | public int perf_runs; 94 | public long perf_cycles; 95 | public long perf_time_us; 96 | 97 | public void* data; 98 | public fixed byte padding[8]; 99 | } 100 | 101 | // computation graph 102 | public unsafe struct ggml_cgraph { 103 | const int GGML_MAX_NODES = 4096; 104 | public int n_nodes; 105 | public int n_leafs; 106 | public int n_threads; 107 | 108 | public nuint work_size; 109 | public ggml_tensor * work; 110 | 111 | // struct ggml_tensor * nodes[GGML_MAX_NODES]; 112 | public Buffer4096 nodes; 113 | // struct ggml_tensor * grads[GGML_MAX_NODES]; 114 | public Buffer4096 grads; 115 | // struct ggml_tensor * leafs[GGML_MAX_NODES]; 116 | public Buffer4096 leafs; 117 | 118 | // performance 119 | public int perf_runs; 120 | public long perf_cycles; 121 | public long perf_time_us; 122 | 123 | public unsafe static ggml_tensor* get_node(ggml_cgraph* graph, int index) 124 | { 125 | return ((ggml_tensor**)&graph->nodes)[index]; 126 | } 127 | 128 | public unsafe static void set_node(ggml_cgraph* graph, int index, ggml_tensor* value) 129 | { 130 | ((ggml_tensor**)&graph->nodes)[index] = value; 131 | } 132 | 133 | public unsafe static ggml_tensor* get_leaf(ggml_cgraph* graph, int index) 134 | { 135 | return ((ggml_tensor**)&graph->leafs)[index]; 136 | } 137 | 138 | public unsafe static void set_leaf(ggml_cgraph* graph, int index, ggml_tensor* value) 139 | { 140 | ((ggml_tensor**)&graph->leafs)[index] = value; 141 | } 142 | 143 | public unsafe static ggml_tensor* get_grad(ggml_cgraph* graph, int index) 144 | { 145 | return ((ggml_tensor**)&graph->grads)[index]; 146 | } 147 | 148 | public unsafe static void set_grad(ggml_cgraph* graph, int index, ggml_tensor* value) 149 | { 150 | ((ggml_tensor**)&graph->grads)[index] = value; 151 | } 152 | }; 153 | public enum ggml_type 154 | { 155 | GGML_TYPE_F32 = 0, 156 | GGML_TYPE_F16 = 1, 157 | GGML_TYPE_Q4_0 = 2, 158 | GGML_TYPE_Q4_1 = 3, 159 | GGML_TYPE_Q4_2 = 4, 160 | GGML_TYPE_Q4_3 = 5, 161 | GGML_TYPE_Q5_0 = 6, 162 | GGML_TYPE_Q5_1 = 7, 163 | GGML_TYPE_Q8_0 = 8, 164 | GGML_TYPE_Q8_1 = 9, 165 | GGML_TYPE_I8, 166 | GGML_TYPE_I16, 167 | GGML_TYPE_I32, 168 | GGML_TYPE_COUNT, 169 | } 170 | 171 | // available tensor operations: 172 | public enum ggml_op 173 | { 174 | GGML_OP_NONE = 0, 175 | 176 | GGML_OP_DUP, 177 | GGML_OP_ADD, 178 | GGML_OP_SUB, 179 | GGML_OP_MUL, 180 | GGML_OP_DIV, 181 | GGML_OP_SQR, 182 | GGML_OP_SQRT, 183 | GGML_OP_SUM, 184 | GGML_OP_MEAN, 185 | GGML_OP_REPEAT, 186 | GGML_OP_ABS, 187 | GGML_OP_SGN, 188 | GGML_OP_NEG, 189 | GGML_OP_STEP, 190 | GGML_OP_RELU, 191 | GGML_OP_GELU, 192 | GGML_OP_SILU, 193 | GGML_OP_NORM, // normalize 194 | GGML_OP_RMS_NORM, 195 | 196 | GGML_OP_MUL_MAT, 197 | 198 | GGML_OP_SCALE, 199 | GGML_OP_CPY, 200 | GGML_OP_CONT, 201 | GGML_OP_RESHAPE, 202 | GGML_OP_VIEW, 203 | GGML_OP_PERMUTE, 204 | GGML_OP_TRANSPOSE, 205 | GGML_OP_GET_ROWS, 206 | GGML_OP_DIAG_MASK_INF, 207 | GGML_OP_SOFT_MAX, 208 | GGML_OP_ROPE, 209 | GGML_OP_ALIBI, 210 | GGML_OP_CONV_1D_1S, 211 | GGML_OP_CONV_1D_2S, 212 | 213 | GGML_OP_FLASH_ATTN, 214 | GGML_OP_FLASH_FF, 215 | 216 | GGML_OP_MAP_UNARY, 217 | GGML_OP_MAP_BINARY, 218 | 219 | GGML_OP_COUNT, 220 | } 221 | 222 | public unsafe struct ggml_state 223 | { 224 | const int GGML_MAX_CONTEXTS = 64; 225 | public Buffer64 contexts; 226 | //public fixed ggml_context_container contexts[GGML_MAX_CONTEXTS]; 227 | } 228 | 229 | public struct ggml_context_container 230 | { 231 | public bool used; 232 | 233 | public ggml_context context; 234 | } 235 | 236 | internal unsafe struct block_q4_0 237 | { 238 | const int QK4_0 = 32; 239 | public float d; // delta 240 | public fixed byte qs[QK4_0 / 2]; // nibbles / quants 241 | }; 242 | internal unsafe struct block_q4_1 243 | { 244 | const int QK4_1 = 32; 245 | public float d; // delta 246 | public float m; // min 247 | public fixed byte qs[QK4_1 / 2]; // nibbles / quants 248 | }; 249 | internal unsafe struct block_q4_2 250 | { 251 | const int QK4_2 = 16; 252 | public ushort d; // delta 253 | public fixed byte qs[QK4_2 / 2]; // nibbles / quants 254 | }; 255 | internal unsafe struct block_q4_3 256 | { 257 | const int QK4_3 = 16; 258 | public ushort d; // delta 259 | public ushort m; // min 260 | public fixed byte qs[QK4_3 / 2]; // nibbles / quants 261 | }; 262 | internal unsafe struct block_q5_0 263 | { 264 | const int QK5_0 = 32; 265 | public Half d; // delta 266 | public fixed byte qh[4]; // 5-th bit of quants 267 | public fixed byte qs[QK5_0 / 2]; // nibbles / quants 268 | }; 269 | internal unsafe struct block_q5_1 270 | { 271 | const int QK5_1 = 32; 272 | public ushort d; // delta 273 | public ushort m; // min 274 | public fixed byte qh[4]; // 5-th bit of quants 275 | public fixed byte qs[QK5_1 / 2]; // nibbles / quants 276 | }; 277 | internal unsafe struct block_q8_0 278 | { 279 | const int QK8_0 = 32; 280 | public float d; // delta 281 | public fixed byte qs[QK8_0]; // quants 282 | }; 283 | internal unsafe struct block_q8_1 284 | { 285 | const int QK8_1 = 32; 286 | public float d; // delta 287 | public float s0; // d * sum(qs[i]) low 288 | public float s1; // d * sum(qs[i]) high 289 | public fixed byte qs[QK8_1]; // quants 290 | }; 291 | 292 | public enum ggml_task_type 293 | { 294 | GGML_TASK_INIT = 0, 295 | GGML_TASK_COMPUTE, 296 | GGML_TASK_FINALIZE, 297 | }; 298 | 299 | public unsafe struct ggml_compute_params 300 | { 301 | public ggml_task_type type; 302 | 303 | public int ith, nth; 304 | 305 | // work buffer for all threads 306 | public nuint wsize; 307 | public void* wdata; 308 | }; 309 | 310 | public struct ggml_compute_state_shared 311 | { 312 | // ggml_lock_t spin 313 | public int spin; 314 | 315 | public int n_threads; 316 | 317 | // synchronization primitives 318 | public volatile int n_ready; 319 | public volatile int has_work; 320 | public volatile int stop; // stop all threads 321 | }; 322 | 323 | public unsafe class ggml_compute_state 324 | { 325 | // ggml_thread_t thrd; 326 | public Thread thrd; 327 | 328 | public ggml_compute_params @params; 329 | public ggml_tensor * node; 330 | 331 | public ggml_compute_state_shared * shared; 332 | }; 333 | 334 | public unsafe struct quantize_fns_t 335 | { 336 | public delegate* unmanaged dequantize_row_q; 337 | public delegate* unmanaged quantize_row_q; 338 | public delegate* unmanaged quantize_row_q_reference; 339 | public delegate* unmanaged quantize_row_q_dot; 340 | public delegate* unmanaged vec_dot_q; 341 | public ggml_type vec_dot_type; 342 | }; 343 | 344 | // 345 | // optimization 346 | // 347 | 348 | // optimization methods 349 | public enum ggml_opt_type { 350 | GGML_OPT_ADAM, 351 | GGML_OPT_LBFGS, 352 | }; 353 | 354 | // linesearch methods 355 | public enum ggml_linesearch { 356 | GGML_LINESEARCH_DEFAULT = 1, 357 | 358 | GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0, 359 | GGML_LINESEARCH_BACKTRACKING_WOLFE = 1, 360 | GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2, 361 | }; 362 | 363 | // optimization return values 364 | public enum ggml_opt_result { 365 | GGML_OPT_OK = 0, 366 | GGML_OPT_DID_NOT_CONVERGE, 367 | GGML_OPT_NO_CONTEXT, 368 | GGML_OPT_INVALID_WOLFE, 369 | GGML_OPT_FAIL, 370 | 371 | GGML_LINESEARCH_FAIL = -128, 372 | GGML_LINESEARCH_MINIMUM_STEP, 373 | GGML_LINESEARCH_MAXIMUM_STEP, 374 | GGML_LINESEARCH_MAXIMUM_ITERATIONS, 375 | GGML_LINESEARCH_INVALID_PARAMETERS, 376 | }; 377 | 378 | // ADAM parameters 379 | public unsafe struct ggml_opt_params_adam 380 | { 381 | public int n_iter; 382 | 383 | public float alpha; // learning rate 384 | public float beta1; 385 | public float beta2; 386 | public float eps; // epsilon for numerical stability 387 | public float eps_f; // epsilon for convergence test 388 | public float eps_g; // epsilon for convergence test 389 | } 390 | 391 | // LBFGS parameters 392 | public unsafe struct ggml_opt_params_lbfgs { 393 | public int m; // number of corrections to approximate the inv. Hessian 394 | public int n_iter; 395 | public int max_linesearch; 396 | 397 | public float eps; // convergence tolerance 398 | public float ftol; // line search tolerance 399 | public float wolfe; 400 | public float min_step; 401 | public float max_step; 402 | 403 | public ggml_linesearch linesearch; 404 | } 405 | 406 | // optimization parameters 407 | // 408 | // see ggml.c (ggml_opt_default_params) for default values 409 | // 410 | public unsafe struct ggml_opt_params { 411 | public ggml_opt_type type; 412 | 413 | public int n_threads; 414 | 415 | // delta-based convergence test 416 | // 417 | // if past == 0 - disabled 418 | // if past > 0: 419 | // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|) 420 | // 421 | public int past; 422 | public float delta; 423 | 424 | // maximum number of iterations without improvement 425 | // 426 | // if 0 - disabled 427 | // if > 0: 428 | // assume convergence if no cost improvement in this number of iterations 429 | // 430 | public int max_no_improvement; 431 | 432 | public bool print_forward_graph; 433 | public bool print_backward_graph; 434 | 435 | public ggml_opt_params_adam adam; 436 | 437 | public ggml_opt_params_lbfgs lbfgs; 438 | }; -------------------------------------------------------------------------------- /Test1/Program.cs: -------------------------------------------------------------------------------- 1 | using GGMLSharp; 2 | using System.Diagnostics; 3 | using static GGMLSharp.Ggml; 4 | 5 | unsafe 6 | { 7 | ggml_init_params init_params = default; 8 | { 9 | init_params.mem_size = 128 * 1024 * 1024; 10 | init_params.mem_buffer = null; 11 | init_params.no_alloc = false; 12 | }; 13 | 14 | ggml_context* ctx0 = ggml_init(init_params); 15 | 16 | { 17 | ggml_tensor *x = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 18 | 19 | ggml_set_param(ctx0, x); 20 | 21 | ggml_tensor *a = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 22 | ggml_tensor *b = ggml_mul(ctx0, x, x); 23 | ggml_tensor *f = ggml_mul(ctx0, b, a); 24 | 25 | // a*x^2 26 | // 2*a*x 27 | 28 | ggml_print_objects(ctx0); 29 | 30 | ggml_cgraph gf = ggml_build_forward(f); 31 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 32 | 33 | ggml_set_f32(x, 2.0f); 34 | ggml_set_f32(a, 3.0f); 35 | 36 | ggml_graph_reset(&gf); 37 | ggml_set_f32(f->grad, 1.0f); 38 | 39 | ggml_graph_compute(ctx0, &gb); 40 | 41 | Console.WriteLine("f = {0:F6}", ggml_get_f32_1d(f, 0)); 42 | Console.WriteLine("df/dx = {0:F6}", ggml_get_f32_1d(x->grad, 0)); 43 | 44 | Debug.Assert(ggml_get_f32_1d(f, 0) == 12.0f); 45 | Debug.Assert(ggml_get_f32_1d(x->grad, 0) == 12.0f); 46 | 47 | ggml_set_f32(x, 3.0f); 48 | 49 | ggml_graph_reset(&gf); 50 | ggml_set_f32(f->grad, 1.0f); 51 | 52 | ggml_graph_compute(ctx0, &gb); 53 | 54 | Console.WriteLine("f = {0:F6}", ggml_get_f32_1d(f, 0)); 55 | Console.WriteLine("df/dx = {0:F6}", ggml_get_f32_1d(x->grad, 0)); 56 | 57 | Debug.Assert(ggml_get_f32_1d(f, 0) == 27.0f); 58 | Debug.Assert(ggml_get_f32_1d(x->grad, 0) == 18.0f); 59 | 60 | ggml_graph_dump_dot(&gf, null, "test1-1-forward.dot"); 61 | ggml_graph_dump_dot(&gb, &gf, "test1-1-backward.dot"); 62 | } 63 | 64 | /////////////////////////////////////////////////////////////// 65 | 66 | { 67 | ggml_tensor * x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 68 | ggml_tensor * x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 69 | ggml_tensor * x3 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 70 | 71 | ggml_set_f32(x1, 3.0f); 72 | ggml_set_f32(x2, 1.0f); 73 | ggml_set_f32(x3, 0.0f); 74 | 75 | ggml_set_param(ctx0, x1); 76 | ggml_set_param(ctx0, x2); 77 | 78 | ggml_tensor * y = ggml_add(ctx0, ggml_mul(ctx0, x1, x1), ggml_mul(ctx0, x1, x2)); 79 | 80 | ggml_cgraph gf = ggml_build_forward(y); 81 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 82 | 83 | ggml_graph_reset(&gf); 84 | ggml_set_f32(y->grad, 1.0f); 85 | 86 | ggml_graph_compute(ctx0, &gb); 87 | 88 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 89 | Console.WriteLine("df/dx1 = {0:F6}", ggml_get_f32_1d(x1->grad, 0)); 90 | Console.WriteLine("df/dx2 = {0:F6}", ggml_get_f32_1d(x2->grad, 0)); 91 | 92 | Debug.Assert(ggml_get_f32_1d(y, 0) == 12.0f); 93 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 7.0f); 94 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 3.0f); 95 | 96 | ggml_tensor * g1 = x1->grad; 97 | ggml_tensor * g2 = x2->grad; 98 | 99 | ggml_cgraph gbb = ggml_build_backward(ctx0, &gb, true); 100 | 101 | ggml_graph_reset(&gb); 102 | ggml_set_f32(g1->grad, 1.0f); 103 | ggml_set_f32(g2->grad, 1.0f); 104 | 105 | ggml_graph_compute(ctx0, &gbb); 106 | 107 | Console.WriteLine("H * [1, 1] = [ {0:F6} {1:F6} ]\n", ggml_get_f32_1d(x1->grad, 0), ggml_get_f32_1d(x2->grad, 0)); 108 | 109 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 3.0f); 110 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 1.0f); 111 | 112 | ggml_graph_dump_dot(&gf, null, "test1-2-forward.dot"); 113 | ggml_graph_dump_dot(&gb, &gf, "test1-2-backward.dot"); 114 | } 115 | 116 | /////////////////////////////////////////////////////////////// 117 | 118 | { 119 | ggml_tensor* x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 120 | ggml_tensor* x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 121 | 122 | ggml_set_param(ctx0, x1); 123 | ggml_set_param(ctx0, x2); 124 | 125 | ggml_tensor* y = ggml_mul(ctx0, ggml_add(ctx0, ggml_mul(ctx0, x1, x1), ggml_mul(ctx0, x1, x2)), x1); 126 | 127 | ggml_cgraph gf = ggml_build_forward(y); 128 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 129 | 130 | ggml_set_f32(x1, 3.0f); 131 | ggml_set_f32(x2, 4.0f); 132 | 133 | ggml_graph_reset(&gf); 134 | ggml_set_f32(y->grad, 1.0f); 135 | 136 | ggml_graph_compute(ctx0, &gb); 137 | 138 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 139 | Console.WriteLine("df/dx1 = {0:F6}", ggml_get_f32_1d(x1->grad, 0)); 140 | Console.WriteLine("df/dx2 = {0:F6}", ggml_get_f32_1d(x2->grad, 0)); 141 | 142 | Debug.Assert(ggml_get_f32_1d(y, 0) == 63.0f); 143 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 51.0f); 144 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 9.0f); 145 | 146 | ggml_graph_dump_dot(&gf, null, "test1-3-forward.dot"); 147 | ggml_graph_dump_dot(&gb, &gf, "test1-3-backward.dot"); 148 | } 149 | 150 | /////////////////////////////////////////////////////////////// 151 | 152 | { 153 | ggml_tensor* x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 154 | ggml_tensor* x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 155 | ggml_tensor* x3 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 1); 156 | 157 | ggml_set_param(ctx0, x1); 158 | ggml_set_param(ctx0, x2); 159 | ggml_set_param(ctx0, x3); 160 | 161 | ggml_tensor* y = ggml_mul(ctx0, ggml_mul(ctx0, ggml_mul(ctx0, x1, x1), ggml_mul(ctx0, x2, x2)), x3); 162 | 163 | ggml_cgraph gf = ggml_build_forward(y); 164 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 165 | 166 | ggml_set_f32(x1, 1.0f); 167 | ggml_set_f32(x2, 2.0f); 168 | ggml_set_f32(x3, 3.0f); 169 | 170 | ggml_graph_reset(&gf); 171 | ggml_set_f32(y->grad, 1.0f); 172 | 173 | ggml_graph_compute(ctx0, &gb); 174 | 175 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 176 | Console.WriteLine("df/dx1 = {0:F6}", ggml_get_f32_1d(x1->grad, 0)); 177 | Console.WriteLine("df/dx2 = {0:F6}", ggml_get_f32_1d(x2->grad, 0)); 178 | Console.WriteLine("df/dx3 = {0:F6}", ggml_get_f32_1d(x3->grad, 0)); 179 | 180 | Debug.Assert(ggml_get_f32_1d(y, 0) == 12.0f); 181 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 24.0f); 182 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 12.0f); 183 | Debug.Assert(ggml_get_f32_1d(x3->grad, 0) == 4.0f); 184 | 185 | ggml_tensor* g1 = x1->grad; 186 | ggml_tensor* g2 = x2->grad; 187 | ggml_tensor* g3 = x3->grad; 188 | 189 | ggml_cgraph gbb = ggml_build_backward(ctx0, &gb, true); 190 | 191 | ggml_graph_reset(&gb); 192 | ggml_set_f32(g1->grad, 1.0f); 193 | ggml_set_f32(g2->grad, 1.0f); 194 | ggml_set_f32(g3->grad, 1.0f); 195 | 196 | ggml_graph_compute(ctx0, &gbb); 197 | 198 | Console.WriteLine("H * [1, 1, 1] = [ {0:F6} {1:F6} {2:F6} ]\n", 199 | ggml_get_f32_1d(x1->grad, 0), 200 | ggml_get_f32_1d(x2->grad, 0), 201 | ggml_get_f32_1d(x3->grad, 0)); 202 | 203 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 56.0f); 204 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 34.0f); 205 | Debug.Assert(ggml_get_f32_1d(x3->grad, 0) == 12.0f); 206 | 207 | ggml_graph_dump_dot(&gf, null, "test1-4-forward.dot"); 208 | ggml_graph_dump_dot(&gb, &gf, "test1-4-backward.dot"); 209 | } 210 | 211 | /////////////////////////////////////////////////////////////// 212 | 213 | { 214 | ggml_tensor * x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 215 | ggml_tensor * x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 216 | 217 | ggml_set_param(ctx0, x1); 218 | ggml_set_param(ctx0, x2); 219 | 220 | ggml_tensor * y = ggml_sum(ctx0, ggml_mul(ctx0, x1, x2)); 221 | 222 | ggml_cgraph gf = ggml_build_forward(y); 223 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 224 | 225 | ggml_set_f32(x1, 3.0f); 226 | ggml_set_f32(x2, 5.0f); 227 | 228 | ggml_graph_reset(&gf); 229 | ggml_set_f32(y->grad, 1.0f); 230 | 231 | ggml_graph_compute(ctx0, &gb); 232 | 233 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 234 | Console.WriteLine("df/dx1 = {0:F6} {1:F6} {2:F6}", 235 | ggml_get_f32_1d(x1->grad, 0), 236 | ggml_get_f32_1d(x1->grad, 1), 237 | ggml_get_f32_1d(x1->grad, 2)); 238 | Console.WriteLine("df/dx2 = {0:F6} {1:F6} {2:F6}", 239 | ggml_get_f32_1d(x2->grad, 0), 240 | ggml_get_f32_1d(x2->grad, 1), 241 | ggml_get_f32_1d(x2->grad, 2)); 242 | 243 | Debug.Assert(ggml_get_f32_1d(y, 0) == 45.0f); 244 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 5.0f); 245 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 3.0f); 246 | Debug.Assert(ggml_get_f32_1d(x1->grad, 1) == 5.0f); 247 | Debug.Assert(ggml_get_f32_1d(x2->grad, 1) == 3.0f); 248 | Debug.Assert(ggml_get_f32_1d(x1->grad, 2) == 5.0f); 249 | Debug.Assert(ggml_get_f32_1d(x2->grad, 2) == 3.0f); 250 | 251 | ggml_graph_dump_dot(&gf, null, "test1-5-forward.dot"); 252 | ggml_graph_dump_dot(&gb, &gf, "test1-5-backward.dot"); 253 | } 254 | 255 | /////////////////////////////////////////////////////////////// 256 | 257 | { 258 | ggml_tensor* x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 259 | ggml_tensor* x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 260 | 261 | ggml_set_param(ctx0, x1); 262 | ggml_set_param(ctx0, x2); 263 | 264 | ggml_tensor* y = 265 | ggml_sum(ctx0, 266 | ggml_add(ctx0, 267 | ggml_mul(ctx0, x1, x2), 268 | ggml_mul(ctx0, 269 | ggml_repeat(ctx0, ggml_new_f32(ctx0, -2.0f), x1), 270 | ggml_mul(ctx0, x1, x1) 271 | ) 272 | ) 273 | ); 274 | 275 | ggml_cgraph gf = ggml_build_forward(y); 276 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 277 | 278 | ggml_set_f32(x1, 3.0f); 279 | ggml_set_f32(x2, 5.0f); 280 | 281 | ggml_graph_reset(&gf); 282 | ggml_set_f32(y->grad, 1.0f); 283 | 284 | ggml_graph_compute(ctx0, &gb); 285 | 286 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 287 | Console.WriteLine("df/dx1 = {0:F6} {1:F6} {2:F6}", 288 | ggml_get_f32_1d(x1->grad, 0), 289 | ggml_get_f32_1d(x1->grad, 1), 290 | ggml_get_f32_1d(x1->grad, 2)); 291 | Console.WriteLine("df/dx2 = {0:F6} {1:F6} {2:F6}", 292 | ggml_get_f32_1d(x2->grad, 0), 293 | ggml_get_f32_1d(x2->grad, 1), 294 | ggml_get_f32_1d(x2->grad, 2)); 295 | 296 | Debug.Assert(ggml_get_f32_1d(y, 0) == -9.0f); 297 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == -7.0f); 298 | Debug.Assert(ggml_get_f32_1d(x1->grad, 1) == -7.0f); 299 | Debug.Assert(ggml_get_f32_1d(x1->grad, 2) == -7.0f); 300 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 3.0f); 301 | Debug.Assert(ggml_get_f32_1d(x2->grad, 1) == 3.0f); 302 | Debug.Assert(ggml_get_f32_1d(x2->grad, 2) == 3.0f); 303 | 304 | ggml_graph_dump_dot(&gf, null, "test1-6-forward.dot"); 305 | ggml_graph_dump_dot(&gb, &gf, "test1-6-backward.dot"); 306 | } 307 | 308 | /////////////////////////////////////////////////////////////// 309 | 310 | { 311 | ggml_tensor * x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 312 | ggml_tensor * x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 313 | 314 | ggml_set_param(ctx0, x1); 315 | ggml_set_param(ctx0, x2); 316 | 317 | ggml_tensor * y = 318 | ggml_sum(ctx0, 319 | ggml_sub(ctx0, 320 | ggml_mul(ctx0, x1, x2), 321 | ggml_mul(ctx0, 322 | ggml_mul(ctx0, x1, x1), 323 | ggml_repeat(ctx0, ggml_new_f32(ctx0, -2.0f), x1) 324 | ) 325 | ) 326 | ); 327 | 328 | ggml_cgraph gf = ggml_build_forward(y); 329 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 330 | 331 | ggml_set_f32(x1, 3.0f); 332 | ggml_set_f32(x2, 5.0f); 333 | 334 | ggml_graph_reset(&gf); 335 | ggml_set_f32(y->grad, 1.0f); 336 | 337 | ggml_graph_compute(ctx0, &gb); 338 | 339 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 340 | Console.WriteLine("df/dx1 = {0:F6} {1:F6} {2:F6}", 341 | ggml_get_f32_1d(x1->grad, 0), 342 | ggml_get_f32_1d(x1->grad, 1), 343 | ggml_get_f32_1d(x1->grad, 2)); 344 | Console.WriteLine("df/dx2 = {0:F6} {1:F6} {2:F6}", 345 | ggml_get_f32_1d(x2->grad, 0), 346 | ggml_get_f32_1d(x2->grad, 1), 347 | ggml_get_f32_1d(x2->grad, 2)); 348 | 349 | Debug.Assert(ggml_get_f32_1d(y, 0) == 99.0f); 350 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 17.0f); 351 | Debug.Assert(ggml_get_f32_1d(x1->grad, 1) == 17.0f); 352 | Debug.Assert(ggml_get_f32_1d(x1->grad, 2) == 17.0f); 353 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 3.0f); 354 | Debug.Assert(ggml_get_f32_1d(x2->grad, 1) == 3.0f); 355 | Debug.Assert(ggml_get_f32_1d(x2->grad, 2) == 3.0f); 356 | 357 | ggml_graph_dump_dot(&gf, null, "test1-7-forward.dot"); 358 | ggml_graph_dump_dot(&gb, &gf, "test1-7-backward.dot"); 359 | } 360 | 361 | /////////////////////////////////////////////////////////////// 362 | 363 | { 364 | ggml_tensor* x1 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 365 | ggml_tensor* x2 = ggml_new_tensor_1d(ctx0, ggml_type.GGML_TYPE_F32, 3); 366 | 367 | ggml_set_param(ctx0, x1); 368 | ggml_set_param(ctx0, x2); 369 | 370 | ggml_tensor* y = 371 | ggml_abs(ctx0, 372 | ggml_sub(ctx0, x1, x2) 373 | ); 374 | 375 | ggml_cgraph gf = ggml_build_forward(y); 376 | ggml_cgraph gb = ggml_build_backward(ctx0, &gf, false); 377 | 378 | ggml_set_f32(x1, 3.0f); 379 | ggml_set_f32(x2, 5.0f); 380 | 381 | ggml_graph_reset(&gf); 382 | ggml_set_f32(y->grad, 1.0f); 383 | 384 | ggml_graph_compute(ctx0, &gb); 385 | 386 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 387 | Console.WriteLine("df/dx1 = {0:F6} {1:F6} {2:F6}", 388 | ggml_get_f32_1d(x1->grad, 0), 389 | ggml_get_f32_1d(x1->grad, 1), 390 | ggml_get_f32_1d(x1->grad, 2)); 391 | Console.WriteLine("df/dx2 = {0:F6} {1:F6} {2:F6}", 392 | ggml_get_f32_1d(x2->grad, 0), 393 | ggml_get_f32_1d(x2->grad, 1), 394 | ggml_get_f32_1d(x2->grad, 2)); 395 | 396 | Debug.Assert(ggml_get_f32_1d(y, 0) == 2.0f); 397 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == -1.0f); 398 | Debug.Assert(ggml_get_f32_1d(x1->grad, 1) == -1.0f); 399 | Debug.Assert(ggml_get_f32_1d(x1->grad, 2) == -1.0f); 400 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == 1.0f); 401 | Debug.Assert(ggml_get_f32_1d(x2->grad, 1) == 1.0f); 402 | Debug.Assert(ggml_get_f32_1d(x2->grad, 2) == 1.0f); 403 | 404 | ggml_set_f32(x1, 7.0f); 405 | ggml_set_f32(x2, 5.0f); 406 | 407 | ggml_graph_reset(&gf); 408 | ggml_set_f32(y->grad, 1.0f); 409 | 410 | ggml_graph_compute(ctx0, &gb); 411 | 412 | Console.WriteLine("y = {0:F6}", ggml_get_f32_1d(y, 0)); 413 | Console.WriteLine("df/dx1 = {0:F6} {1:F6} {2:F6}", 414 | ggml_get_f32_1d(x1->grad, 0), 415 | ggml_get_f32_1d(x1->grad, 1), 416 | ggml_get_f32_1d(x1->grad, 2)); 417 | Console.WriteLine("df/dx2 = {0:F6} {1:F6} {2:F6}", 418 | ggml_get_f32_1d(x2->grad, 0), 419 | ggml_get_f32_1d(x2->grad, 1), 420 | ggml_get_f32_1d(x2->grad, 2)); 421 | 422 | Debug.Assert(ggml_get_f32_1d(y, 0) == 2.0f); 423 | Debug.Assert(ggml_get_f32_1d(x1->grad, 0) == 1.0f); 424 | Debug.Assert(ggml_get_f32_1d(x1->grad, 1) == 1.0f); 425 | Debug.Assert(ggml_get_f32_1d(x1->grad, 2) == 1.0f); 426 | Debug.Assert(ggml_get_f32_1d(x2->grad, 0) == -1.0f); 427 | Debug.Assert(ggml_get_f32_1d(x2->grad, 1) == -1.0f); 428 | Debug.Assert(ggml_get_f32_1d(x2->grad, 2) == -1.0f); 429 | 430 | ggml_graph_dump_dot(&gf, null, "test1-8-forward.dot"); 431 | ggml_graph_dump_dot(&gb, &gf, "test1-8-backward.dot"); 432 | } 433 | 434 | ggml_free(ctx0); 435 | 436 | return 0; 437 | } --------------------------------------------------------------------------------