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%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLResource.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 "MTLAllocation.hpp"25#include "MTLDefines.hpp"26#include "MTLHeaderBridge.hpp"27#include "MTLPrivate.hpp"28#include <mach/mach.h>2930namespace MTL31{32class Device;33class Heap;34_MTL_ENUM(NS::UInteger, PurgeableState) {35 PurgeableStateKeepCurrent = 1,36 PurgeableStateNonVolatile = 2,37 PurgeableStateVolatile = 3,38 PurgeableStateEmpty = 4,39};4041_MTL_ENUM(NS::UInteger, CPUCacheMode) {42 CPUCacheModeDefaultCache = 0,43 CPUCacheModeWriteCombined = 1,44};4546_MTL_ENUM(NS::UInteger, StorageMode) {47 StorageModeShared = 0,48 StorageModeManaged = 1,49 StorageModePrivate = 2,50 StorageModeMemoryless = 3,51};5253_MTL_ENUM(NS::UInteger, HazardTrackingMode) {54 HazardTrackingModeDefault = 0,55 HazardTrackingModeUntracked = 1,56 HazardTrackingModeTracked = 2,57};5859_MTL_ENUM(NS::Integer, SparsePageSize) {60 SparsePageSize16 = 101,61 SparsePageSize64 = 102,62 SparsePageSize256 = 103,63};6465_MTL_ENUM(NS::Integer, BufferSparseTier) {66 BufferSparseTierNone = 0,67 BufferSparseTier1 = 1,68};6970_MTL_ENUM(NS::Integer, TextureSparseTier) {71 TextureSparseTierNone = 0,72 TextureSparseTier1 = 1,73 TextureSparseTier2 = 2,74};7576_MTL_OPTIONS(NS::UInteger, ResourceOptions) {77 ResourceCPUCacheModeDefaultCache = 0x0,78 ResourceCPUCacheModeWriteCombined = 0x1,79 ResourceStorageModeShared = 0x0,80 ResourceStorageModeManaged = 0x10,81 ResourceStorageModePrivate = 0x20,82 ResourceStorageModeMemoryless = 0x30,83 ResourceHazardTrackingModeDefault = 0x0,84 ResourceHazardTrackingModeUntracked = 0x100,85 ResourceHazardTrackingModeTracked = 0x200,86 ResourceOptionCPUCacheModeDefault [[deprecated("use MTL::ResourceCPUCacheModeDefaultCache instead")]] = ResourceCPUCacheModeDefaultCache,87 ResourceOptionCPUCacheModeWriteCombined [[deprecated("use MTL::ResourceCPUCacheModeWriteCombined instead")]] = ResourceCPUCacheModeWriteCombined,88};8990class Resource : public NS::Referencing<Resource, Allocation>91{92public:93 NS::UInteger allocatedSize() const;9495 CPUCacheMode cpuCacheMode() const;9697 Device* device() const;9899 HazardTrackingMode hazardTrackingMode() const;100101 Heap* heap() const;102 NS::UInteger heapOffset() const;103104 bool isAliasable();105106 NS::String* label() const;107108 void makeAliasable();109110 ResourceOptions resourceOptions() const;111112 void setLabel(const NS::String* label);113114 kern_return_t setOwner(task_id_token_t task_id_token);115116 PurgeableState setPurgeableState(MTL::PurgeableState state);117118 StorageMode storageMode() const;119};120121}122_MTL_INLINE NS::UInteger MTL::Resource::allocatedSize() const123{124 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(allocatedSize));125}126127_MTL_INLINE MTL::CPUCacheMode MTL::Resource::cpuCacheMode() const128{129 return Object::sendMessage<MTL::CPUCacheMode>(this, _MTL_PRIVATE_SEL(cpuCacheMode));130}131132_MTL_INLINE MTL::Device* MTL::Resource::device() const133{134 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));135}136137_MTL_INLINE MTL::HazardTrackingMode MTL::Resource::hazardTrackingMode() const138{139 return Object::sendMessage<MTL::HazardTrackingMode>(this, _MTL_PRIVATE_SEL(hazardTrackingMode));140}141142_MTL_INLINE MTL::Heap* MTL::Resource::heap() const143{144 return Object::sendMessage<MTL::Heap*>(this, _MTL_PRIVATE_SEL(heap));145}146147_MTL_INLINE NS::UInteger MTL::Resource::heapOffset() const148{149 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(heapOffset));150}151152_MTL_INLINE bool MTL::Resource::isAliasable()153{154 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(isAliasable));155}156157_MTL_INLINE NS::String* MTL::Resource::label() const158{159 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));160}161162_MTL_INLINE void MTL::Resource::makeAliasable()163{164 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(makeAliasable));165}166167_MTL_INLINE MTL::ResourceOptions MTL::Resource::resourceOptions() const168{169 return Object::sendMessage<MTL::ResourceOptions>(this, _MTL_PRIVATE_SEL(resourceOptions));170}171172_MTL_INLINE void MTL::Resource::setLabel(const NS::String* label)173{174 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);175}176177_MTL_INLINE kern_return_t MTL::Resource::setOwner(task_id_token_t task_id_token)178{179 return Object::sendMessage<kern_return_t>(this, _MTL_PRIVATE_SEL(setOwnerWithIdentity_), task_id_token);180}181182_MTL_INLINE MTL::PurgeableState MTL::Resource::setPurgeableState(MTL::PurgeableState state)183{184 return Object::sendMessage<MTL::PurgeableState>(this, _MTL_PRIVATE_SEL(setPurgeableState_), state);185}186187_MTL_INLINE MTL::StorageMode MTL::Resource::storageMode() const188{189 return Object::sendMessage<MTL::StorageMode>(this, _MTL_PRIVATE_SEL(storageMode));190}191