spb/forge Public MIT
Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.
C++ 61.2%
C 23%
Python 7.6%
TeX 7.2%
CMake 1.1%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLBinaryArchive.hpp4//5// Copyright 2020-2025 Apple Inc.6//7// Licensed under the Apache License, Version 2.0 (the "License");8// you may not use this file except in compliance with the License.9// You may obtain a copy of the License at10//11// http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// distributed under the License is distributed on an "AS IS" BASIS,15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16// See the License for the specific language governing permissions and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223#include "../Foundation/Foundation.hpp"24#include "MTLDefines.hpp"25#include "MTLHeaderBridge.hpp"26#include "MTLPrivate.hpp"2728namespace MTL29{30class BinaryArchiveDescriptor;31class ComputePipelineDescriptor;32class Device;33class FunctionDescriptor;34class Library;35class MeshRenderPipelineDescriptor;36class RenderPipelineDescriptor;37class StitchedLibraryDescriptor;38class TileRenderPipelineDescriptor;39_MTL_ENUM(NS::UInteger, BinaryArchiveError) {40 BinaryArchiveErrorNone = 0,41 BinaryArchiveErrorInvalidFile = 1,42 BinaryArchiveErrorUnexpectedElement = 2,43 BinaryArchiveErrorCompilationFailure = 3,44 BinaryArchiveErrorInternalError = 4,45};4647_MTL_CONST(NS::ErrorDomain, BinaryArchiveDomain);48class BinaryArchiveDescriptor : public NS::Copying<BinaryArchiveDescriptor>49{50public:51 static BinaryArchiveDescriptor* alloc();5253 BinaryArchiveDescriptor* init();5455 void setUrl(const NS::URL* url);56 NS::URL* url() const;57};58class BinaryArchive : public NS::Referencing<BinaryArchive>59{60public:61 bool addComputePipelineFunctions(const MTL::ComputePipelineDescriptor* descriptor, NS::Error** error);6263 bool addFunction(const MTL::FunctionDescriptor* descriptor, const MTL::Library* library, NS::Error** error);6465 bool addLibrary(const MTL::StitchedLibraryDescriptor* descriptor, NS::Error** error);6667 bool addMeshRenderPipelineFunctions(const MTL::MeshRenderPipelineDescriptor* descriptor, NS::Error** error);6869 bool addRenderPipelineFunctions(const MTL::RenderPipelineDescriptor* descriptor, NS::Error** error);7071 bool addTileRenderPipelineFunctions(const MTL::TileRenderPipelineDescriptor* descriptor, NS::Error** error);7273 Device* device() const;7475 NS::String* label() const;7677 bool serializeToURL(const NS::URL* url, NS::Error** error);7879 void setLabel(const NS::String* label);80};8182}83_MTL_PRIVATE_DEF_CONST(NS::ErrorDomain, BinaryArchiveDomain);84_MTL_INLINE MTL::BinaryArchiveDescriptor* MTL::BinaryArchiveDescriptor::alloc()85{86 return NS::Object::alloc<MTL::BinaryArchiveDescriptor>(_MTL_PRIVATE_CLS(MTLBinaryArchiveDescriptor));87}8889_MTL_INLINE MTL::BinaryArchiveDescriptor* MTL::BinaryArchiveDescriptor::init()90{91 return NS::Object::init<MTL::BinaryArchiveDescriptor>();92}9394_MTL_INLINE void MTL::BinaryArchiveDescriptor::setUrl(const NS::URL* url)95{96 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setUrl_), url);97}9899_MTL_INLINE NS::URL* MTL::BinaryArchiveDescriptor::url() const100{101 return Object::sendMessage<NS::URL*>(this, _MTL_PRIVATE_SEL(url));102}103104_MTL_INLINE bool MTL::BinaryArchive::addComputePipelineFunctions(const MTL::ComputePipelineDescriptor* descriptor, NS::Error** error)105{106 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addComputePipelineFunctionsWithDescriptor_error_), descriptor, error);107}108109_MTL_INLINE bool MTL::BinaryArchive::addFunction(const MTL::FunctionDescriptor* descriptor, const MTL::Library* library, NS::Error** error)110{111 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addFunctionWithDescriptor_library_error_), descriptor, library, error);112}113114_MTL_INLINE bool MTL::BinaryArchive::addLibrary(const MTL::StitchedLibraryDescriptor* descriptor, NS::Error** error)115{116 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addLibraryWithDescriptor_error_), descriptor, error);117}118119_MTL_INLINE bool MTL::BinaryArchive::addMeshRenderPipelineFunctions(const MTL::MeshRenderPipelineDescriptor* descriptor, NS::Error** error)120{121 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addMeshRenderPipelineFunctionsWithDescriptor_error_), descriptor, error);122}123124_MTL_INLINE bool MTL::BinaryArchive::addRenderPipelineFunctions(const MTL::RenderPipelineDescriptor* descriptor, NS::Error** error)125{126 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addRenderPipelineFunctionsWithDescriptor_error_), descriptor, error);127}128129_MTL_INLINE bool MTL::BinaryArchive::addTileRenderPipelineFunctions(const MTL::TileRenderPipelineDescriptor* descriptor, NS::Error** error)130{131 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(addTileRenderPipelineFunctionsWithDescriptor_error_), descriptor, error);132}133134_MTL_INLINE MTL::Device* MTL::BinaryArchive::device() const135{136 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));137}138139_MTL_INLINE NS::String* MTL::BinaryArchive::label() const140{141 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));142}143144_MTL_INLINE bool MTL::BinaryArchive::serializeToURL(const NS::URL* url, NS::Error** error)145{146 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(serializeToURL_error_), url, error);147}148149_MTL_INLINE void MTL::BinaryArchive::setLabel(const NS::String* label)150{151 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);152}153