Posts

Post not yet marked as solved
3 Replies
0 Views
Hi there, many thanks for the quick response. Below is an example encodeToCommandBuffer function, required by Core ML for Custom layers to run on the GPU - (BOOL) encodeToCommandBuffer:(id<MTLCommandBuffer>)commandBuffer inputs:(NSArray<id<MTLTexture>> *)inputs outputs:(NSArray<id<MTLTexture>> *)outputs error:(NSError *__autoreleasing _Nullable *)error{       id<MTLBuffer> test_buffer = [PSO.device newBufferWithLength:(8) options:MTLResourceStorageModeShared];   id <MTLBlitCommandEncoder> blitCommandEncoder = [commandBuffer blitCommandEncoder];   [blitCommandEncoder copyFromTexture:inputs[0]               sourceSlice:0               sourceLevel:0               sourceOrigin:MTLOriginMake(0, 0, 0)                sourceSize:MTLSizeMake(1, 1, 1)                 toBuffer:test_buffer            destinationOffset:0          destinationBytesPerRow:8         destinationBytesPerImage:8];   [blitCommandEncoder endEncoding];   id<MTLComputeCommandEncoder> computeEncoder = [commandBuffer computeCommandEncoderWithDispatchType:MTLDispatchTypeSerial];   assert(computeEncoder != nil);   [computeEncoder setComputePipelineState:PSO];   [computeEncoder setTexture:inputs[0] atIndex:0]; // Input   [computeEncoder setTexture:outputs[0] atIndex:2]; // Output       MTLSize dimThreadsBlock, dimThreadsGrid;       dimThreadsGrid = MTLSizeMake(1, 1, 1);   dimThreadsBlock = MTLSizeMake(1, 1, 1);   [computeEncoder dispatchThreads: dimThreadsGrid threadsPerThreadgroup:dimThreadsBlock];       [computeEncoder endEncoding]; Note that the above does not include [commandBuffer commit]; or [commandBuffer waitUntilCompleted];. If these are added an error occurs, stating; failed assertion _status < MTLCommandBufferStatusCommitted at line 300 in -[IOGPUMetalCommandBuffer setCurrentCommandEncoder:] I can run code on the GPU successfully for my Custom Layer without explicitly committing the commandBuffer within the encodeToCommandBuffer function, this make me believe this is handled by Core ML internally (or so the name of the function would imply)? Note that all setup for GPU is done prior to above function being called, in the initWithParameterDictionary function required by Core ML; - (instancetype) initWithParameterDictionary:(NSDictionary<NSString *,id> *)parameters error:(NSError *__autoreleasing _Nullable *)error{  //kernel_height = [parameters[@"kernel_height"] intValue];  //kernel_width = [parameters[@"kernel_width"] intValue];      //NSLog(@"kh kw %@", [parameters allKeys]);  //self = [super init]; // This line might introduce breaks !!!!!  // Metal GPU setup  id<MTLDevice> device = MTLCreateSystemDefaultDevice();  NSError* error_pso = nil;      id<MTLLibrary> defaultlibrary = [device newDefaultLibrary];  if (defaultlibrary == nil){   NSLog(@"Failed to find the default library");   return nil;  }      id<MTLFunction> function = [defaultlibrary newFunctionWithName:@"kernel"];  if (rans_decode == nil){   NSLog(@"Failed to find the function");   return nil;  } PSO = [device newComputePipelineStateWithFunction:function error:&error_pso];  if (PSO == nil || error_pso != nil){   NSLog(@"Failed to find the default library");   return nil;  }      return self; } Thanks again!