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%
4.8 KB · 135 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTL4CommandEncoder.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 "MTLCommandEncoder.hpp"25#include "MTLDefines.hpp"26#include "MTLHeaderBridge.hpp"27#include "MTLPrivate.hpp"2829namespace MTL430{31class CommandBuffer;32}3334namespace MTL35{36class Fence;37}3839namespace MTL440{41_MTL_OPTIONS(NS::UInteger, VisibilityOptions) {42    VisibilityOptionNone = 0,43    VisibilityOptionDevice = 1,44    VisibilityOptionResourceAlias = 1 << 1,45};4647class CommandEncoder : public NS::Referencing<CommandEncoder>48{49public:50    void           barrierAfterEncoderStages(MTL::Stages afterEncoderStages, MTL::Stages beforeEncoderStages, MTL4::VisibilityOptions visibilityOptions);5152    void           barrierAfterQueueStages(MTL::Stages afterQueueStages, MTL::Stages beforeStages, MTL4::VisibilityOptions visibilityOptions);5354    void           barrierAfterStages(MTL::Stages afterStages, MTL::Stages beforeQueueStages, MTL4::VisibilityOptions visibilityOptions);5556    CommandBuffer* commandBuffer() const;5758    void           endEncoding();5960    void           insertDebugSignpost(const NS::String* string);6162    NS::String*    label() const;6364    void           popDebugGroup();6566    void           pushDebugGroup(const NS::String* string);6768    void           setLabel(const NS::String* label);6970    void           updateFence(const MTL::Fence* fence, MTL::Stages afterEncoderStages);7172    void           waitForFence(const MTL::Fence* fence, MTL::Stages beforeEncoderStages);73};7475}76_MTL_INLINE void MTL4::CommandEncoder::barrierAfterEncoderStages(MTL::Stages afterEncoderStages, MTL::Stages beforeEncoderStages, MTL4::VisibilityOptions visibilityOptions)77{78    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(barrierAfterEncoderStages_beforeEncoderStages_visibilityOptions_), afterEncoderStages, beforeEncoderStages, visibilityOptions);79}8081_MTL_INLINE void MTL4::CommandEncoder::barrierAfterQueueStages(MTL::Stages afterQueueStages, MTL::Stages beforeStages, MTL4::VisibilityOptions visibilityOptions)82{83    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(barrierAfterQueueStages_beforeStages_visibilityOptions_), afterQueueStages, beforeStages, visibilityOptions);84}8586_MTL_INLINE void MTL4::CommandEncoder::barrierAfterStages(MTL::Stages afterStages, MTL::Stages beforeQueueStages, MTL4::VisibilityOptions visibilityOptions)87{88    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(barrierAfterStages_beforeQueueStages_visibilityOptions_), afterStages, beforeQueueStages, visibilityOptions);89}9091_MTL_INLINE MTL4::CommandBuffer* MTL4::CommandEncoder::commandBuffer() const92{93    return Object::sendMessage<MTL4::CommandBuffer*>(this, _MTL_PRIVATE_SEL(commandBuffer));94}9596_MTL_INLINE void MTL4::CommandEncoder::endEncoding()97{98    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(endEncoding));99}100101_MTL_INLINE void MTL4::CommandEncoder::insertDebugSignpost(const NS::String* string)102{103    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(insertDebugSignpost_), string);104}105106_MTL_INLINE NS::String* MTL4::CommandEncoder::label() const107{108    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));109}110111_MTL_INLINE void MTL4::CommandEncoder::popDebugGroup()112{113    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(popDebugGroup));114}115116_MTL_INLINE void MTL4::CommandEncoder::pushDebugGroup(const NS::String* string)117{118    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(pushDebugGroup_), string);119}120121_MTL_INLINE void MTL4::CommandEncoder::setLabel(const NS::String* label)122{123    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);124}125126_MTL_INLINE void MTL4::CommandEncoder::updateFence(const MTL::Fence* fence, MTL::Stages afterEncoderStages)127{128    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(updateFence_afterEncoderStages_), fence, afterEncoderStages);129}130131_MTL_INLINE void MTL4::CommandEncoder::waitForFence(const MTL::Fence* fence, MTL::Stages beforeEncoderStages)132{133    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitForFence_beforeEncoderStages_), fence, beforeEncoderStages);134}135