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%
5.6 KB · 179 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLResidencySet.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>2829namespace MTL30{31class Allocation;32class Device;33class ResidencySetDescriptor;3435class ResidencySetDescriptor : public NS::Copying<ResidencySetDescriptor>36{37public:38    static ResidencySetDescriptor* alloc();3940    ResidencySetDescriptor*        init();41    NS::UInteger                   initialCapacity() const;4243    NS::String*                    label() const;4445    void                           setInitialCapacity(NS::UInteger initialCapacity);4647    void                           setLabel(const NS::String* label);48};49class ResidencySet : public NS::Referencing<ResidencySet>50{51public:52    void         addAllocation(const MTL::Allocation* allocation);53    void         addAllocations(const MTL::Allocation* const allocations[], NS::UInteger count);5455    NS::Array*   allAllocations() const;5657    uint64_t     allocatedSize() const;5859    NS::UInteger allocationCount() const;6061    void         commit();6263    bool         containsAllocation(const MTL::Allocation* anAllocation);6465    Device*      device() const;6667    void         endResidency();6869    NS::String*  label() const;7071    void         removeAllAllocations();7273    void         removeAllocation(const MTL::Allocation* allocation);74    void         removeAllocations(const MTL::Allocation* const allocations[], NS::UInteger count);7576    void         requestResidency();77};7879}80_MTL_INLINE MTL::ResidencySetDescriptor* MTL::ResidencySetDescriptor::alloc()81{82    return NS::Object::alloc<MTL::ResidencySetDescriptor>(_MTL_PRIVATE_CLS(MTLResidencySetDescriptor));83}8485_MTL_INLINE MTL::ResidencySetDescriptor* MTL::ResidencySetDescriptor::init()86{87    return NS::Object::init<MTL::ResidencySetDescriptor>();88}8990_MTL_INLINE NS::UInteger MTL::ResidencySetDescriptor::initialCapacity() const91{92    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(initialCapacity));93}9495_MTL_INLINE NS::String* MTL::ResidencySetDescriptor::label() const96{97    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));98}99100_MTL_INLINE void MTL::ResidencySetDescriptor::setInitialCapacity(NS::UInteger initialCapacity)101{102    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInitialCapacity_), initialCapacity);103}104105_MTL_INLINE void MTL::ResidencySetDescriptor::setLabel(const NS::String* label)106{107    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);108}109110_MTL_INLINE void MTL::ResidencySet::addAllocation(const MTL::Allocation* allocation)111{112    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addAllocation_), allocation);113}114115_MTL_INLINE void MTL::ResidencySet::addAllocations(const MTL::Allocation* const allocations[], NS::UInteger count)116{117    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addAllocations_count_), allocations, count);118}119120_MTL_INLINE NS::Array* MTL::ResidencySet::allAllocations() const121{122    return Object::sendMessage<NS::Array*>(this, _MTL_PRIVATE_SEL(allAllocations));123}124125_MTL_INLINE uint64_t MTL::ResidencySet::allocatedSize() const126{127    return Object::sendMessage<uint64_t>(this, _MTL_PRIVATE_SEL(allocatedSize));128}129130_MTL_INLINE NS::UInteger MTL::ResidencySet::allocationCount() const131{132    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(allocationCount));133}134135_MTL_INLINE void MTL::ResidencySet::commit()136{137    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit));138}139140_MTL_INLINE bool MTL::ResidencySet::containsAllocation(const MTL::Allocation* anAllocation)141{142    return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(containsAllocation_), anAllocation);143}144145_MTL_INLINE MTL::Device* MTL::ResidencySet::device() const146{147    return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));148}149150_MTL_INLINE void MTL::ResidencySet::endResidency()151{152    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(endResidency));153}154155_MTL_INLINE NS::String* MTL::ResidencySet::label() const156{157    return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));158}159160_MTL_INLINE void MTL::ResidencySet::removeAllAllocations()161{162    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeAllAllocations));163}164165_MTL_INLINE void MTL::ResidencySet::removeAllocation(const MTL::Allocation* allocation)166{167    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeAllocation_), allocation);168}169170_MTL_INLINE void MTL::ResidencySet::removeAllocations(const MTL::Allocation* const allocations[], NS::UInteger count)171{172    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeAllocations_count_), allocations, count);173}174175_MTL_INLINE void MTL::ResidencySet::requestResidency()176{177    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(requestResidency));178}179