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%
8.8 KB · 244 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLCounters.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"27#include "MTLResource.hpp"28#include <cstdint>2930namespace MTL31{32class CounterSampleBufferDescriptor;33class CounterSet;34class Device;35_MTL_ENUM(NS::Integer, CounterSampleBufferError) {36    CounterSampleBufferErrorOutOfMemory = 0,37    CounterSampleBufferErrorInvalid = 1,38    CounterSampleBufferErrorInternal = 2,39};4041using CommonCounter = NS::String*;42using CommonCounterSet = NS::String*;4344static const NS::UInteger CounterErrorValue = static_cast<NS::UInteger>(~0ULL);45static const NS::UInteger CounterDontSample = static_cast<NS::UInteger>(-1);46_MTL_CONST(NS::ErrorDomain, CounterErrorDomain);47_MTL_CONST(CommonCounter, CommonCounterTimestamp);48_MTL_CONST(CommonCounter, CommonCounterTessellationInputPatches);49_MTL_CONST(CommonCounter, CommonCounterVertexInvocations);50_MTL_CONST(CommonCounter, CommonCounterPostTessellationVertexInvocations);51_MTL_CONST(CommonCounter, CommonCounterClipperInvocations);52_MTL_CONST(CommonCounter, CommonCounterClipperPrimitivesOut);53_MTL_CONST(CommonCounter, CommonCounterFragmentInvocations);54_MTL_CONST(CommonCounter, CommonCounterFragmentsPassed);55_MTL_CONST(CommonCounter, CommonCounterComputeKernelInvocations);56_MTL_CONST(CommonCounter, CommonCounterTotalCycles);57_MTL_CONST(CommonCounter, CommonCounterVertexCycles);58_MTL_CONST(CommonCounter, CommonCounterTessellationCycles);59_MTL_CONST(CommonCounter, CommonCounterPostTessellationVertexCycles);60_MTL_CONST(CommonCounter, CommonCounterFragmentCycles);61_MTL_CONST(CommonCounter, CommonCounterRenderTargetWriteCycles);62_MTL_CONST(CommonCounterSet, CommonCounterSetTimestamp);63_MTL_CONST(CommonCounterSet, CommonCounterSetStageUtilization);64_MTL_CONST(CommonCounterSet, CommonCounterSetStatistic);65struct CounterResultTimestamp66{67    uint64_t timestamp;68} _MTL_PACKED;6970struct CounterResultStageUtilization71{72    uint64_t totalCycles;73    uint64_t vertexCycles;74    uint64_t tessellationCycles;75    uint64_t postTessellationVertexCycles;76    uint64_t fragmentCycles;77    uint64_t renderTargetCycles;78} _MTL_PACKED;7980struct CounterResultStatistic81{82    uint64_t tessellationInputPatches;83    uint64_t vertexInvocations;84    uint64_t postTessellationVertexInvocations;85    uint64_t clipperInvocations;86    uint64_t clipperPrimitivesOut;87    uint64_t fragmentInvocations;88    uint64_t fragmentsPassed;89    uint64_t computeKernelInvocations;90} _MTL_PACKED;9192class Counter : public NS::Referencing<Counter>93{94public:95    NS::String* name() const;96};97class CounterSet : public NS::Referencing<CounterSet>98{99public:100    NS::Array*  counters() const;101102    NS::String* name() const;103};104class CounterSampleBufferDescriptor : public NS::Copying<CounterSampleBufferDescriptor>105{106public:107    static CounterSampleBufferDescriptor* alloc();108109    CounterSet*                           counterSet() const;110111    CounterSampleBufferDescriptor*        init();112113    NS::String*                           label() const;114115    NS::UInteger                          sampleCount() const;116117    void                                  setCounterSet(const MTL::CounterSet* counterSet);118119    void                                  setLabel(const NS::String* label);120121    void                                  setSampleCount(NS::UInteger sampleCount);122123    void                                  setStorageMode(MTL::StorageMode storageMode);124    StorageMode                           storageMode() const;125};126class CounterSampleBuffer : public NS::Referencing<CounterSampleBuffer>127{128public:129    Device*      device() const;130131    NS::String*  label() const;132133    NS::Data*    resolveCounterRange(NS::Range range);134135    NS::UInteger sampleCount() const;136};137138}139140_MTL_PRIVATE_DEF_CONST(NS::ErrorDomain, CounterErrorDomain);141_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterTimestamp);142_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterTessellationInputPatches);143_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterVertexInvocations);144_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterPostTessellationVertexInvocations);145_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterClipperInvocations);146_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterClipperPrimitivesOut);147_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterFragmentInvocations);148_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterFragmentsPassed);149_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterComputeKernelInvocations);150_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterTotalCycles);151_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterVertexCycles);152_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterTessellationCycles);153_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterPostTessellationVertexCycles);154_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterFragmentCycles);155_MTL_PRIVATE_DEF_CONST(MTL::CommonCounter, CommonCounterRenderTargetWriteCycles);156_MTL_PRIVATE_DEF_CONST(MTL::CommonCounterSet, CommonCounterSetTimestamp);157_MTL_PRIVATE_DEF_CONST(MTL::CommonCounterSet, CommonCounterSetStageUtilization);158_MTL_PRIVATE_DEF_CONST(MTL::CommonCounterSet, CommonCounterSetStatistic);159160_MTL_INLINE NS::String* MTL::Counter::name() const161{162    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(name));163}164165_MTL_INLINE NS::Array* MTL::CounterSet::counters() const166{167    return Object::sendMessage<NS::Array*>(this, _MTL_PRIVATE_SEL(counters));168}169170_MTL_INLINE NS::String* MTL::CounterSet::name() const171{172    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(name));173}174175_MTL_INLINE MTL::CounterSampleBufferDescriptor* MTL::CounterSampleBufferDescriptor::alloc()176{177    return NS::Object::alloc<MTL::CounterSampleBufferDescriptor>(_MTL_PRIVATE_CLS(MTLCounterSampleBufferDescriptor));178}179180_MTL_INLINE MTL::CounterSet* MTL::CounterSampleBufferDescriptor::counterSet() const181{182    return Object::sendMessage<MTL::CounterSet*>(this, _MTL_PRIVATE_SEL(counterSet));183}184185_MTL_INLINE MTL::CounterSampleBufferDescriptor* MTL::CounterSampleBufferDescriptor::init()186{187    return NS::Object::init<MTL::CounterSampleBufferDescriptor>();188}189190_MTL_INLINE NS::String* MTL::CounterSampleBufferDescriptor::label() const191{192    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));193}194195_MTL_INLINE NS::UInteger MTL::CounterSampleBufferDescriptor::sampleCount() const196{197    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(sampleCount));198}199200_MTL_INLINE void MTL::CounterSampleBufferDescriptor::setCounterSet(const MTL::CounterSet* counterSet)201{202    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setCounterSet_), counterSet);203}204205_MTL_INLINE void MTL::CounterSampleBufferDescriptor::setLabel(const NS::String* label)206{207    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);208}209210_MTL_INLINE void MTL::CounterSampleBufferDescriptor::setSampleCount(NS::UInteger sampleCount)211{212    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSampleCount_), sampleCount);213}214215_MTL_INLINE void MTL::CounterSampleBufferDescriptor::setStorageMode(MTL::StorageMode storageMode)216{217    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setStorageMode_), storageMode);218}219220_MTL_INLINE MTL::StorageMode MTL::CounterSampleBufferDescriptor::storageMode() const221{222    return Object::sendMessage<MTL::StorageMode>(this, _MTL_PRIVATE_SEL(storageMode));223}224225_MTL_INLINE MTL::Device* MTL::CounterSampleBuffer::device() const226{227    return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));228}229230_MTL_INLINE NS::String* MTL::CounterSampleBuffer::label() const231{232    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));233}234235_MTL_INLINE NS::Data* MTL::CounterSampleBuffer::resolveCounterRange(NS::Range range)236{237    return Object::sendMessage<NS::Data*>(this, _MTL_PRIVATE_SEL(resolveCounterRange_), range);238}239240_MTL_INLINE NS::UInteger MTL::CounterSampleBuffer::sampleCount() const241{242    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(sampleCount));243}244