Repository: ggerganov/llama.cpp
File: ggml/src/ggml-metal/ggml-metal.m (Objective-C)

Code before:
    /*.has_simdgroup_reduction =*/ false,
    /*.has_simdgroup_mm        =*/ false,
    /*.has_residency_sets      =*/ false,
    /*.has_bfloat              =*/ false,
    /*.use_bfloat              =*/ false,
    /*.use_fusion              =*/ true,
    /*.debug_fusion            =*/ 0,
    /*.fuse_cnt                =*/ { 0 },
    /*.max_size                =*/ 0,
    /*.name                    =*/ "",
};

// acquire
static id<MTLDevice> ggml_backend_metal_device_acq(struct ggml_backend_metal_device_context * ctx) {
    assert(ctx != NULL);

    if (ctx->mtl_lock == nil) {
        ctx->mtl_lock = [[NSLock alloc] init];
    }

    if (ctx->mtl_device == nil) {
        ctx->mtl_device = MTLCreateSystemDefaultDevice();
    }

    // There may be still no Metal device available
    if (ctx->mtl_device) {
        ctx->has_simdgroup_reduction  = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
        ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];

        ctx->has_simdgroup_mm = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];

#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
        ctx->has_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil;
#endif

        ctx->has_bfloat  = [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
        ctx->has_bfloat |= [ctx->mtl_device supportsFamily:MTLGPUFamilyApple6];

#if defined(GGML_METAL_USE_BF16)
        ctx->use_bfloat = ctx->has_bfloat;
#else
        ctx->use_bfloat = false;
#endif
        ctx->use_fusion = getenv("GGML_METAL_FUSION_DISABLE") == nil;

        {
            const char * val = getenv("GGML_METAL_FUSION_DEBUG");
            ctx->debug_fusion = val ? atoi(val) : 0;
        }

        memset(ctx->fuse_cnt, 0, sizeof(ctx->fuse_cnt));

Review comment (suggestion):
The context initialization should be done only once:

```suggestion
    if (ctx->mtl_device == nil) {
        ctx->mtl_device = MTLCreateSystemDefaultDevice();

        // init the context if we got a device
        if (ctx->mtl_device) {
```

Code after:
    /*.has_simdgroup_reduction =*/ false,
    /*.has_simdgroup_mm        =*/ false,
    /*.has_residency_sets      =*/ false,
    /*.has_bfloat              =*/ false,
    /*.use_bfloat              =*/ false,
    /*.use_fusion              =*/ true,
    /*.debug_fusion            =*/ 0,
    /*.fuse_cnt                =*/ { 0 },
    /*.max_size                =*/ 0,
    /*.name                    =*/ "",
};

// acquire
static id<MTLDevice> ggml_backend_metal_device_acq(struct ggml_backend_metal_device_context * ctx) {
    assert(ctx != NULL);

    if (ctx->mtl_lock == nil) {
        ctx->mtl_lock = [[NSLock alloc] init];
    }

    if (ctx->mtl_device == nil) {
        ctx->mtl_device = MTLCreateSystemDefaultDevice();

        if (ctx->mtl_device) {
            ctx->has_simdgroup_reduction  = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];
            ctx->has_simdgroup_reduction |= [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];

            ctx->has_simdgroup_mm = [ctx->mtl_device supportsFamily:MTLGPUFamilyApple7];

#if defined(GGML_METAL_HAS_RESIDENCY_SETS)
            ctx->has_residency_sets = getenv("GGML_METAL_NO_RESIDENCY") == nil;
#endif

            ctx->has_bfloat  = [ctx->mtl_device supportsFamily:MTLGPUFamilyMetal3_GGML];
            ctx->has_bfloat |= [ctx->mtl_device supportsFamily:MTLGPUFamilyApple6];

#if defined(GGML_METAL_USE_BF16)
            ctx->use_bfloat = ctx->has_bfloat;
#else
            ctx->use_bfloat = false;
#endif
            ctx->use_fusion = getenv("GGML_METAL_FUSION_DISABLE") == nil;

            {
                const char * val = getenv("GGML_METAL_FUSION_DEBUG");
                ctx->debug_fusion = val ? atoi(val) : 0;
            }

            memset(ctx->fuse_cnt, 0, sizeof(ctx->fuse_cnt));

            ctx->max_size = ctx->mtl_device.maxBufferLength;