{"id":25198168,"url":"https://github.com/andrewcodedev/metaphor","last_synced_at":"2025-05-08T22:44:31.461Z","repository":{"id":223368530,"uuid":"759575408","full_name":"andrewCodeDev/Metaphor","owner":"andrewCodeDev","description":"Machine Learning Library in Zig","archived":false,"fork":false,"pushed_at":"2024-07-10T07:40:53.000Z","size":73196,"stargazers_count":85,"open_issues_count":3,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-08T22:44:26.644Z","etag":null,"topics":["zig-library"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrewCodeDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-18T22:50:03.000Z","updated_at":"2025-03-28T00:46:56.000Z","dependencies_parsed_at":"2024-06-29T00:29:31.666Z","dependency_job_id":"3df3956c-47e1-4527-9c0c-25f7ca674db5","html_url":"https://github.com/andrewCodeDev/Metaphor","commit_stats":null,"previous_names":["andrewcodedev/metaphor"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewCodeDev%2FMetaphor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewCodeDev%2FMetaphor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewCodeDev%2FMetaphor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewCodeDev%2FMetaphor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrewCodeDev","download_url":"https://codeload.github.com/andrewCodeDev/Metaphor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["zig-library"],"created_at":"2025-02-10T02:41:09.961Z","updated_at":"2025-05-08T22:44:31.432Z","avatar_url":"https://github.com/andrewCodeDev.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Metaphor\n\nMetaphor is a Zig based machine-learning library. The goal is to be fully featured and feel pythonic without sacrificing the low-level control of Zig.\n\nThis library is in a highly experimental state and will likely change a lot. Contributions and feedback are welcome!\n\n# How does Metaphor work?\n\nMetaphor is a stream based library. Streams are work queues that can be created on your GPU (much like threads):\n\n```zig\n// To start Metaphor, you must initialize the\n// device for the GPU device you want to use\n// Initialize device and on device zero:\n\nmp.device.init(0);\n\n// Open the stream you want to compute on.\n// Streams can be run in parallel to launch\n// multiple kernels simultaneously:\n\nconst stream = mp.stream.init();\ndefer mp.stream.deinit(stream);\n\n// wait until all work is done on a stream\nmp.streams.synchronize(stream);\n\n//////////////////////////////////////////////\n\n// alternatively, you can create stream-groups:\nconst streams = mp.stream.Group(3).init();\ndefer streams.deinit();\n\n// get your streams\nconst s0 = streams.items[0];\n\n// wait until all streams are synchronized\nstreams.synchronize();\n```\n\nOnce you've setup your streams, you can create your graph. The graph manages your tensor data and can track operations that occur:\n\n```zig\nconst G = mp.Graph.init(.{\n    .stream = stream,\n    .mode = train,\n});\ndefer G.deinit();\n```\n\nNow, we can build tensors. Tensors are the unit of computation in Metaphor:\n\n```zig\n// tensors are freed on graph.deinit()...\nconst X1 = G.tensor(.inp, .r32, mp.Rank(2){ 2, 2 });\nconst X2 = G.tensor(.wgt, .r32, mp.Rank(2){ 2, 2 });\n\n// but they can be safely freed before deinit like this:\n// X1.free();\n```\n\nThere are several namespaces within Metaphor, including the \"ops\", \"mem\", \"scalar\", \"loss\", and more.\n\nThe Metaphor device utilities (which including copying to the device, creating streams, synchronization, etc...) can be used independently of machine learning!\n\n```zig\n// fill with a single value (casted automatically)\nmp.mem.fill(X1, 2);\n\n// sequence all elements from 0.0 in increments of 1.0\nmp.mem.sequence(X2, 0.0, 1.0);\n```\n\nMetaphor expresses dimensional operations inspired by the Einsteinian convention. All strings are parsed at compile time - no runtime overhead!\n\n```zig\n// y = A.x\nconst y = mp.ops.innerProduct(A, x, \"ij,j-\u003ei\");\n\n// y = A.x + b\nconst y = mp.ops.linear(A, x, b, \"ij,j-\u003ei\");\n\n// B = A transpose\nconst B = mp.ops.permutate(A, \"ij-\u003eji\");\n\n// w = u + v\nconst w = mp.ops.add(u, v);\n\n// operations can be composed, e = (a + b) * (c + d)\nconst e = mp.ops.hadamard(mp.ops.add(a, b), mp.ops.add(c, d));\n```\n\nReversal is straight-forward, but has many additional features (see src/examples for more):\n\n```zig\n// feed-forward block\nconst y = mp.ops.selu(mp.ops.linear(x, A, b, \"i,ij-\u003ej\"));\n\n// if we want to free the hidden nodes, we can use\n// \".free\" - this does not free weights or inputs.\n// to keep hidden nodes, we can use \".keep\"\ny.reverse(.free);\n\n// inspect gradients\nif (A.grads()) |grd| {\n    // use gradient...\n}\n```\n\n# Installation Steps\n\nIf you do not have a version of the GCC compiler, download/install it and locate the path it was saved to.\n\nIf you do not have a version of the CUDA Developer Toolkit, download it and locate the path it was saved to.\n\nClone this repository onto your local machine.\n\nEither copy, move, or symlink the CUDA Developer Toolkit to the \"Metaphor/deps\" folder. Metaphor uses this path to ensure that CUDA includes are correct.\n\nOpen the \"Metaphor/config.json\" file and provide your own values for the following:\n\n```json\n{\n    \"gcc-bin-path\": \"/usr/bin/gcc\",\n    \"gpu-architecture\": \"sm_89\"\n}\n```\n\nYou can now use Metaphor!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewcodedev%2Fmetaphor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrewcodedev%2Fmetaphor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewcodedev%2Fmetaphor/lists"}