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%
3.9 KB · 120 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLBuffer.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 "MTLGPUAddress.hpp"26#include "MTLHeaderBridge.hpp"27#include "MTLPrivate.hpp"28#include "MTLResource.hpp"2930namespace MTL31{32class Buffer;33class Device;34class Tensor;35class TensorDescriptor;36class Texture;37class TextureDescriptor;3839class Buffer : public NS::Referencing<Buffer, Resource>40{41public:42    void             addDebugMarker(const NS::String* marker, NS::Range range);4344    void*            contents();4546    void             didModifyRange(NS::Range range);4748    GPUAddress       gpuAddress() const;4950    NS::UInteger     length() const;5152    Buffer*          newRemoteBufferViewForDevice(const MTL::Device* device);5354    Tensor*          newTensor(const MTL::TensorDescriptor* descriptor, NS::UInteger offset, NS::Error** error);5556    Texture*         newTexture(const MTL::TextureDescriptor* descriptor, NS::UInteger offset, NS::UInteger bytesPerRow);5758    Buffer*          remoteStorageBuffer() const;5960    void             removeAllDebugMarkers();6162    BufferSparseTier sparseBufferTier() const;63};6465}66_MTL_INLINE void MTL::Buffer::addDebugMarker(const NS::String* marker, NS::Range range)67{68    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addDebugMarker_range_), marker, range);69}7071_MTL_INLINE void* MTL::Buffer::contents()72{73    return Object::sendMessage<void*>(this, _MTL_PRIVATE_SEL(contents));74}7576_MTL_INLINE void MTL::Buffer::didModifyRange(NS::Range range)77{78    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(didModifyRange_), range);79}8081_MTL_INLINE MTL::GPUAddress MTL::Buffer::gpuAddress() const82{83    return Object::sendMessage<MTL::GPUAddress>(this, _MTL_PRIVATE_SEL(gpuAddress));84}8586_MTL_INLINE NS::UInteger MTL::Buffer::length() const87{88    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(length));89}9091_MTL_INLINE MTL::Buffer* MTL::Buffer::newRemoteBufferViewForDevice(const MTL::Device* device)92{93    return Object::sendMessage<MTL::Buffer*>(this, _MTL_PRIVATE_SEL(newRemoteBufferViewForDevice_), device);94}9596_MTL_INLINE MTL::Tensor* MTL::Buffer::newTensor(const MTL::TensorDescriptor* descriptor, NS::UInteger offset, NS::Error** error)97{98    return Object::sendMessage<MTL::Tensor*>(this, _MTL_PRIVATE_SEL(newTensorWithDescriptor_offset_error_), descriptor, offset, error);99}100101_MTL_INLINE MTL::Texture* MTL::Buffer::newTexture(const MTL::TextureDescriptor* descriptor, NS::UInteger offset, NS::UInteger bytesPerRow)102{103    return Object::sendMessage<MTL::Texture*>(this, _MTL_PRIVATE_SEL(newTextureWithDescriptor_offset_bytesPerRow_), descriptor, offset, bytesPerRow);104}105106_MTL_INLINE MTL::Buffer* MTL::Buffer::remoteStorageBuffer() const107{108    return Object::sendMessage<MTL::Buffer*>(this, _MTL_PRIVATE_SEL(remoteStorageBuffer));109}110111_MTL_INLINE void MTL::Buffer::removeAllDebugMarkers()112{113    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(removeAllDebugMarkers));114}115116_MTL_INLINE MTL::BufferSparseTier MTL::Buffer::sparseBufferTier() const117{118    return Object::sendMessage<MTL::BufferSparseTier>(this, _MTL_PRIVATE_SEL(sparseBufferTier));119}120