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.6 KB · 112 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLLogState.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"2728namespace MTL29{30class LogStateDescriptor;31_MTL_ENUM(NS::Integer, LogLevel) {32    LogLevelUndefined = 0,33    LogLevelDebug = 1,34    LogLevelInfo = 2,35    LogLevelNotice = 3,36    LogLevelError = 4,37    LogLevelFault = 5,38};3940_MTL_ENUM(NS::UInteger, LogStateError) {41    LogStateErrorInvalidSize = 1,42    LogStateErrorInvalid = 2,43};4445using LogHandlerFunction = std::function<void(NS::String* subsystem, NS::String* category, MTL::LogLevel logLevel, NS::String* message)>;4647_MTL_CONST(NS::ErrorDomain, LogStateErrorDomain);48class LogState : public NS::Referencing<LogState>49{50public:51    void addLogHandler(void (^block)(NS::String*, NS::String*, MTL::LogLevel, NS::String*));52    void addLogHandler(const MTL::LogHandlerFunction& handler);53};54class LogStateDescriptor : public NS::Copying<LogStateDescriptor>55{56public:57    static LogStateDescriptor* alloc();5859    NS::Integer                bufferSize() const;6061    LogStateDescriptor*        init();6263    LogLevel                   level() const;6465    void                       setBufferSize(NS::Integer bufferSize);6667    void                       setLevel(MTL::LogLevel level);68};6970}71_MTL_PRIVATE_DEF_CONST(NS::ErrorDomain, LogStateErrorDomain);72_MTL_INLINE void MTL::LogState::addLogHandler(void (^block)(NS::String*, NS::String*, MTL::LogLevel, NS::String*))73{74    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addLogHandler_), block);75}7677_MTL_INLINE void MTL::LogState::addLogHandler(const MTL::LogHandlerFunction& handler)78{79    __block LogHandlerFunction function = handler;80    addLogHandler(^void(NS::String* subsystem, NS::String* category, MTL::LogLevel logLevel, NS::String* message) { function(subsystem, category, logLevel, message); });81}8283_MTL_INLINE MTL::LogStateDescriptor* MTL::LogStateDescriptor::alloc()84{85    return NS::Object::alloc<MTL::LogStateDescriptor>(_MTL_PRIVATE_CLS(MTLLogStateDescriptor));86}8788_MTL_INLINE NS::Integer MTL::LogStateDescriptor::bufferSize() const89{90    return Object::sendMessage<NS::Integer>(this, _MTL_PRIVATE_SEL(bufferSize));91}9293_MTL_INLINE MTL::LogStateDescriptor* MTL::LogStateDescriptor::init()94{95    return NS::Object::init<MTL::LogStateDescriptor>();96}9798_MTL_INLINE MTL::LogLevel MTL::LogStateDescriptor::level() const99{100    return Object::sendMessage<MTL::LogLevel>(this, _MTL_PRIVATE_SEL(level));101}102103_MTL_INLINE void MTL::LogStateDescriptor::setBufferSize(NS::Integer bufferSize)104{105    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setBufferSize_), bufferSize);106}107108_MTL_INLINE void MTL::LogStateDescriptor::setLevel(MTL::LogLevel level)109{110    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLevel_), level);111}112