SPB Git

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%
7.2 KB · 194 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTL4CommandBuffer.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 "MTL4RenderCommandEncoder.hpp"25#include "MTLAccelerationStructureTypes.hpp"26#include "MTLDefines.hpp"27#include "MTLHeaderBridge.hpp"28#include "MTLPrivate.hpp"2930namespace MTL431{32class CommandAllocator;33class CommandBufferOptions;34class ComputeCommandEncoder;35class CounterHeap;36class MachineLearningCommandEncoder;37class RenderCommandEncoder;38class RenderPassDescriptor;39}4041namespace MTL42{43class Device;44class Fence;45class LogState;46class ResidencySet;47}4849namespace MTL450{51class CommandBufferOptions : public NS::Copying<CommandBufferOptions>52{53public:54    static CommandBufferOptions* alloc();5556    CommandBufferOptions*        init();5758    MTL::LogState*               logState() const;59    void                         setLogState(const MTL::LogState* logState);60};61class CommandBuffer : public NS::Referencing<CommandBuffer>62{63public:64    void                           beginCommandBuffer(const MTL4::CommandAllocator* allocator);65    void                           beginCommandBuffer(const MTL4::CommandAllocator* allocator, const MTL4::CommandBufferOptions* options);6667    ComputeCommandEncoder*         computeCommandEncoder();6869    MTL::Device*                   device() const;7071    void                           endCommandBuffer();7273    NS::String*                    label() const;7475    MachineLearningCommandEncoder* machineLearningCommandEncoder();7677    void                           popDebugGroup();7879    void                           pushDebugGroup(const NS::String* string);8081    RenderCommandEncoder*          renderCommandEncoder(const MTL4::RenderPassDescriptor* descriptor);82    RenderCommandEncoder*          renderCommandEncoder(const MTL4::RenderPassDescriptor* descriptor, MTL4::RenderEncoderOptions options);8384    void                           resolveCounterHeap(const MTL4::CounterHeap* counterHeap, NS::Range range, const MTL4::BufferRange bufferRange, const MTL::Fence* fenceToWait, const MTL::Fence* fenceToUpdate);8586    void                           setLabel(const NS::String* label);8788    void                           useResidencySet(const MTL::ResidencySet* residencySet);89    void                           useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count);9091    void                           writeTimestampIntoHeap(const MTL4::CounterHeap* counterHeap, NS::UInteger index);92};9394}95_MTL_INLINE MTL4::CommandBufferOptions* MTL4::CommandBufferOptions::alloc()96{97    return NS::Object::alloc<MTL4::CommandBufferOptions>(_MTL_PRIVATE_CLS(MTL4CommandBufferOptions));98}99100_MTL_INLINE MTL4::CommandBufferOptions* MTL4::CommandBufferOptions::init()101{102    return NS::Object::init<MTL4::CommandBufferOptions>();103}104105_MTL_INLINE MTL::LogState* MTL4::CommandBufferOptions::logState() const106{107    return Object::sendMessage<MTL::LogState*>(this, _MTL_PRIVATE_SEL(logState));108}109110_MTL_INLINE void MTL4::CommandBufferOptions::setLogState(const MTL::LogState* logState)111{112    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLogState_), logState);113}114115_MTL_INLINE void MTL4::CommandBuffer::beginCommandBuffer(const MTL4::CommandAllocator* allocator)116{117    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(beginCommandBufferWithAllocator_), allocator);118}119120_MTL_INLINE void MTL4::CommandBuffer::beginCommandBuffer(const MTL4::CommandAllocator* allocator, const MTL4::CommandBufferOptions* options)121{122    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(beginCommandBufferWithAllocator_options_), allocator, options);123}124125_MTL_INLINE MTL4::ComputeCommandEncoder* MTL4::CommandBuffer::computeCommandEncoder()126{127    return Object::sendMessage<MTL4::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoder));128}129130_MTL_INLINE MTL::Device* MTL4::CommandBuffer::device() const131{132    return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));133}134135_MTL_INLINE void MTL4::CommandBuffer::endCommandBuffer()136{137    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(endCommandBuffer));138}139140_MTL_INLINE NS::String* MTL4::CommandBuffer::label() const141{142    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));143}144145_MTL_INLINE MTL4::MachineLearningCommandEncoder* MTL4::CommandBuffer::machineLearningCommandEncoder()146{147    return Object::sendMessage<MTL4::MachineLearningCommandEncoder*>(this, _MTL_PRIVATE_SEL(machineLearningCommandEncoder));148}149150_MTL_INLINE void MTL4::CommandBuffer::popDebugGroup()151{152    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(popDebugGroup));153}154155_MTL_INLINE void MTL4::CommandBuffer::pushDebugGroup(const NS::String* string)156{157    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(pushDebugGroup_), string);158}159160_MTL_INLINE MTL4::RenderCommandEncoder* MTL4::CommandBuffer::renderCommandEncoder(const MTL4::RenderPassDescriptor* descriptor)161{162    return Object::sendMessage<MTL4::RenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(renderCommandEncoderWithDescriptor_), descriptor);163}164165_MTL_INLINE MTL4::RenderCommandEncoder* MTL4::CommandBuffer::renderCommandEncoder(const MTL4::RenderPassDescriptor* descriptor, MTL4::RenderEncoderOptions options)166{167    return Object::sendMessage<MTL4::RenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(renderCommandEncoderWithDescriptor_options_), descriptor, options);168}169170_MTL_INLINE void MTL4::CommandBuffer::resolveCounterHeap(const MTL4::CounterHeap* counterHeap, NS::Range range, const MTL4::BufferRange bufferRange, const MTL::Fence* fenceToWait, const MTL::Fence* fenceToUpdate)171{172    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(resolveCounterHeap_withRange_intoBuffer_waitFence_updateFence_), counterHeap, range, bufferRange, fenceToWait, fenceToUpdate);173}174175_MTL_INLINE void MTL4::CommandBuffer::setLabel(const NS::String* label)176{177    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);178}179180_MTL_INLINE void MTL4::CommandBuffer::useResidencySet(const MTL::ResidencySet* residencySet)181{182    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySet_), residencySet);183}184185_MTL_INLINE void MTL4::CommandBuffer::useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count)186{187    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySets_count_), residencySets, count);188}189190_MTL_INLINE void MTL4::CommandBuffer::writeTimestampIntoHeap(const MTL4::CounterHeap* counterHeap, NS::UInteger index)191{192    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(writeTimestampIntoHeap_atIndex_), counterHeap, index);193}194