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.0 KB · 139 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTL4Counters.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 <cstdint>2829#include <cstdint>3031namespace MTL432{33class CounterHeapDescriptor;34_MTL_ENUM(NS::Integer, CounterHeapType) {35    CounterHeapTypeInvalid,36    CounterHeapTypeTimestamp,37};3839_MTL_ENUM(NS::Integer, TimestampGranularity) {40    TimestampGranularityRelaxed = 0,41    TimestampGranularityPrecise = 1,42};4344struct TimestampHeapEntry45{46    uint64_t timestamp;47} _MTL_PACKED;4849class CounterHeapDescriptor : public NS::Copying<CounterHeapDescriptor>50{51public:52    static CounterHeapDescriptor* alloc();5354    NS::UInteger                  count() const;5556    CounterHeapDescriptor*        init();5758    void                          setCount(NS::UInteger count);5960    void                          setType(MTL4::CounterHeapType type);61    CounterHeapType               type() const;62};63class CounterHeap : public NS::Referencing<CounterHeap>64{65public:66    NS::UInteger    count() const;67    void            invalidateCounterRange(NS::Range range);6869    NS::String*     label() const;7071    NS::Data*       resolveCounterRange(NS::Range range);7273    void            setLabel(const NS::String* label);7475    CounterHeapType type() const;76};7778}7980_MTL_INLINE MTL4::CounterHeapDescriptor* MTL4::CounterHeapDescriptor::alloc()81{82    return NS::Object::alloc<MTL4::CounterHeapDescriptor>(_MTL_PRIVATE_CLS(MTL4CounterHeapDescriptor));83}8485_MTL_INLINE NS::UInteger MTL4::CounterHeapDescriptor::count() const86{87    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(count));88}8990_MTL_INLINE MTL4::CounterHeapDescriptor* MTL4::CounterHeapDescriptor::init()91{92    return NS::Object::init<MTL4::CounterHeapDescriptor>();93}9495_MTL_INLINE void MTL4::CounterHeapDescriptor::setCount(NS::UInteger count)96{97    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setCount_), count);98}99100_MTL_INLINE void MTL4::CounterHeapDescriptor::setType(MTL4::CounterHeapType type)101{102    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setType_), type);103}104105_MTL_INLINE MTL4::CounterHeapType MTL4::CounterHeapDescriptor::type() const106{107    return Object::sendMessage<MTL4::CounterHeapType>(this, _MTL_PRIVATE_SEL(type));108}109110_MTL_INLINE NS::UInteger MTL4::CounterHeap::count() const111{112    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(count));113}114115_MTL_INLINE void MTL4::CounterHeap::invalidateCounterRange(NS::Range range)116{117    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(invalidateCounterRange_), range);118}119120_MTL_INLINE NS::String* MTL4::CounterHeap::label() const121{122    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));123}124125_MTL_INLINE NS::Data* MTL4::CounterHeap::resolveCounterRange(NS::Range range)126{127    return Object::sendMessage<NS::Data*>(this, _MTL_PRIVATE_SEL(resolveCounterRange_), range);128}129130_MTL_INLINE void MTL4::CounterHeap::setLabel(const NS::String* label)131{132    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);133}134135_MTL_INLINE MTL4::CounterHeapType MTL4::CounterHeap::type() const136{137    return Object::sendMessage<MTL4::CounterHeapType>(this, _MTL_PRIVATE_SEL(type));138}139